Re: [PHP] Sessions in classes dosn't work!?
> Hmmm, it seems that "obj" is not saved to the session file. > > > I don't thik that & before new is needed. This is weird :) It worked when I removed the & (thought that I tried that ;). Now I just can't remember why I put it there in the first place. I hope it wasn't importent ... well I will proboply find out when some of the code dosn't work. > 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. I knew about the __sleep() and __wakeup() but not about the returning of an array containing the names of the varibles that needs to be stored. Cool feature - Thanks for the tip :) /watson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Sessions in classes dosn't work!?
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" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> 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?: > > *** > > 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) . "\n"); > print($obj->getFoo()); > session_destroy(); > } else { > session_register("obj"); > $obj =& new TestClass(); > $obj->setFoo("Hello World!"); > print("TYPE: " . gettype($obj) . "\n"); > print("Click here\n"); > } > } > > } > > session_start(); > new TestClass2($state); > > ?> > *** > > When I on "page1" print("TYPE: " . gettype($obj) . "\n"); > I get this output: TYPE: object > > When I on "page2" print("TYPE: " . gettype($obj) . "\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: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
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?: *** foo = $bar; } function getFoo() { return $this->foo; } } class TestClass2 { function TestClass2($state) { global $obj; if($state == "page2") { print("TYPE: " . gettype($obj) . "\n"); print($obj->getFoo()); session_destroy(); } else { session_register("obj"); $obj =& new TestClass(); $obj->setFoo("Hello World!"); print("TYPE: " . gettype($obj) . "\n"); print("Click here\n"); } } } session_start(); new TestClass2($state); ?> *** When I on "page1" print("TYPE: " . gettype($obj) . "\n"); I get this output: TYPE: object When I on "page2" print("TYPE: " . gettype($obj) . "\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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
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. Andrey Hristov IcyGEN Corporation http://www.icygen.com 99% - Original Message - From: "Thomas Watson" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, August 30, 2001 12:10 PM Subject: [PHP] Sessions in classes dosn't work!? > How come that I cannot store a varible in the session when I do this > from within another object? > > This is my code: > > session_start(); > > class MyClass { >function MyClass($state) { > if($state == "page2") { > print("[" . $foo . "]"); > session_destroy(); > } else { > session_register("foo"); > $foo = "Hello World!"; > print("Click here\n"); > } >} > } > > new MyClass($state); > ?> > > > /watson > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]