Passing Variables – Passing Objects in PHP

We've looked at passing variables, because of the stateless nature of html and the Internet. Every internet page that opens starts at ground zero with no prior knowledge of anything that came before it. Any information that page needs has to be passed to the page.

We've done this in a number of ways: with html forms using get and post, with cookies, with sessions, and with includes to pass variables to functions. We've discussed these methods in previous posts. What about objects?

Objects can be passed to another page, but there's a couple of gotcha's that need to be accounted for first. Objects as you know consist of properties, or variables, and methods described in a class. The class describes the structure of any instance of an object created from that class. There's more information in objects to convey, or pass to another file or function, then with a simple variable.

If we passed the object instance like any other variable, the receiving file would not know anything about the object or its class, and the receiving file would just think it was some sort of bit stream coming to it. What we need to do is to encode the object information along with the object instance before we pass the object and decode it when we get to the new location.

Step number one is to make sure that the class is in scope, or visible, to the object instance we are going to pass. This way the class information is available to be passed. Let's make a class and instance and pass it.

First the class...

class carwash {

    private $carmake;
    private $caryear;

    public function getCarmake () {
        return $this->carmake;
    }

    public function setCarmake ($carmake) {
        $this->carmake = $carmake;
    }

    public function getCaryear () {
        return $this->caryear;
    }

    public function setCaryear ($caryear) {
        $this->caryear = $caryear; 

    }

    public function washcar () {
        echo "scruba, dub, dub, scruba, dub, dub";
        echo " I'm feeling cleaner, Thank you.";
    }
}

Even though normally we will keep our classes in separate files, for the sake of brevity lets keep it in the same file and make an instance of the class that we will pass.

    $cartopass = new carwash();

    $cartopass->setCarmake("Buick");
    $cartopass->setCaryear(1999);

We have our object instance, and we have set its parameters to a 1999 Buick. What we want to do is send all of the above variables and class information over to the next file in the form of a long encoded string. We do this with serialization and use a session variable to store the byte encoded string. This is done with one line like so...

    $_SESSION['encoded_cartopass'] = serialize($cartopass) ;

Don't forget to call session_start() at the top of the file. We now have a session variable that is available to us in the receiving page, so let's go there...

The first thing we need to do is unpack, or unencode, the object. Let's do that.

// First let's get the variable from the session

$encoded_cartopass = $_SESSION['encoded_cartopass'];

// Now let's unpack it

$cartopass = unserialize($encoded_cartopass);

// Note we could have done this in one step, like so

$cartopass = unserialize ( $_SESSION['encoded_cartopass'] );

Now we can use the object in the new page. Here's an example:

    echo "Your car is a " . $cartopass->getCarmake ();
    echo "Made in ". $cartopass->getCaryear() . ". ";

// and we can use the class method  

    $cartopass->washcar();

The output on the screen would read. (I put in the html line breaks that don't print out in the code in this post).

    Your car is a Buick
    Made in 1999.
    scruba, dub, dub, scruba, dub, dub
    I'm feeling cleaner, Thank you.

Comments

Passing Variables – Passing Objects in PHP — 4 Comments

  1. Hi,

    I know you posted this about an year ago. But I m stuck in a similar situation any advice or suggestion will help a-lot.

    When I try to use the object from the session in the new page… I get the following error. But the weird part is when I refresh the page it starts working. Any ideas ?!?

    Error: Fatal error: Call to a member function getEmail() on a non-object in

    Cheers,

    . . .

    • Hi,
      That error message usually comes when you have not instantiated an object of the getEmail() class in your original file, or calling the method without a $this->. If you are serializing an object and passing it to a new file, you may be serializing something that doesn’t work and passing it. Make sure the call works in the original file before passing it. I’m guessing the class is in your autoload path and the second reload is finding the object, but not that particular instance.

      • Thank you daleV! That resolved my problem. Appreciate you help.

        Cheers,
        . . .