Re: Sessions in classes dosn't work!?
| From: | Thomas Watson | Date: | Thu, 30 Aug 2001 11:18:05 +0000 |
| Subject: | Re: Sessions in classes dosn't work!? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-65200@lists.php.net to get a copy of this message | ||
> 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