View Full Version : OOP issue

01-18-2010, 12:13 AM
Is it possible to register a session var with the content value of a whole object and its properties, simply using the $this var?


class myclass(){

public function registerSession(){

$_SESSION['myobject']=$this; //$this refers to the whole object


01-18-2010, 02:39 AM
Yes, however you will need to save the object by first serializing the object, and then unserializing the object when the session begins again

serialize() (http://www.php.net/serialize) | unserialize() (http://www.php.net/unserialize)

01-18-2010, 06:14 AM
Not true, PHP will automatically serialize the object. If overridden, it will call the __sleep and __wakeup methods prior to the serialize and unserialize methods. These can be useful (I use the term fairly lightly) if you wanted to store a class that connects to an external resource. Since the resource itself cannot be serialized, you can sleep with the configurations necessary and wakeup with recalling a connection handler. The automatic unserialize call will repopulate any variables required for configurations that were stored when the sleep was called.

So to answer the OP's question, yes you can do just as you have done above. Since you're working in a PHP5 environment, you need not concern yourself with reference handling on $this.