Re: Sessions in classes dosn't work!?

From: Date: Thu, 30 Aug 2001 10:48:30 +0000
Subject: Re: Sessions in classes dosn't work!?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-65213@lists.php.net to get a copy of this message
Hmmm, it seems that "obj" is not saved to the session file. I don't thik that & before new is needed. Also may the problem is in that you use the class contructor. When objects are saved in session, and on other page you want to wakeup them you must provide two methods for the class: function __sleep(){ return array("r","l","p","m_id"); } function __wakeup(){ $this->res_id=mysql_connnect("user","pass","host"); } __sleep() (double under) is called before wrinting in session file. it returns names (as strings) of variables which have to be saved. Why not all, because you can have a $res_id of db_link which is meaningless to save. __wakeup() is called when the object is restored in memory. If you have $res_id you have to create it here, so the persistent object after restoring to be as before saving. Andrey Hristov IcyGEN Corporation http://www.icygen.com 99% ----- Original Message ----- From: "Thomas Watson" <dev.null@tson.dk> To: <php-general@lists.php.net> Sent: Thursday, August 30, 2001 2:18 PM Subject: Re: [PHP] Sessions in classes dosn't work!? > > This is because $foo is local to the member functions of the class. > > declare global $foo at the start of every function. > > Ok thnx... it worked... but how do you then explain this?: > > *********************************************** > <?php > > class TestClass { > > var $foo; > > function setFoo($bar) { > $this->foo = $bar; > } > > function getFoo() { > return $this->foo; > } > > } > > class TestClass2 { > > function TestClass2($state) { > global $obj; > if($state == "page2") { > print("TYPE: " . gettype($obj) . "<br />\n"); > print($obj->getFoo()); > session_destroy(); > } else { > session_register("obj"); > $obj =& new TestClass(); > $obj->setFoo("Hello World!"); > print("TYPE: " . gettype($obj) . "<br />\n"); > print("<a href=\"?state=page2\">Click here</a>\n"); > } > } > > } > > session_start(); > new TestClass2($state); > > ?> > *********************************************** > > When I on "page1" print("TYPE: " . gettype($obj) . "<br > />\n"); > I get this output: TYPE: object > > When I on "page2" print("TYPE: " . gettype($obj) . "<br > />\n"); > I get this output: TYPE: NULL > (and the linie saying print($obj->getFoo()); generates an error: > Fatal error: Call to a member function on a non-object in > /home/httpd/html/test/save_objects.php on line 27 > > (just so you know: I have no problems saving an object in the session when > it's done outside a class. It's when I do this from within a class that the > problem occurs) > > > /watson > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > >

« previous php.general (#65213) next »