note 11753 added to session_register
| From: | php-general at lists dot php dot net | Date: | Tue, 06 Mar 2001 01:34:55 +0000 |
| Subject: | note 11753 added to session_register | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-11917@lists.php.net to get a copy of this message | ||
if you are going to use session variables containing instantiated classes, you must define the
object in the pages where the object is referred to. More importantly, the definition of the object
must precede the session_start() where it is referred to.
<?php
session_start();
session_register("foo");
class foo {
var $bar
}
$foo=new foo;
$foo->bar="test";
?>
WRONG:
===another page===
<?php
session_start();
echo $foo; # prints 'Object'
echo $foo->bar; # produces error message
?>
WRONG:
===another page===
<?php
session_start();
echo $foo; # prints 'Object'
echo $foo->bar; # produces error message
class foo{
var $bar
} # can't define the class after referring to properties or methods within it
?>
RIGHT:
===another page===
<?php
class foo {
var $bar
}
session_start();
echo $foo; # prints 'Object'
echo $foo->bar; # prints "test"
?>
for obvious reasons, its best to put your class definitions in a separate document, and include it
in both documents.
http://www.php.net/manual/en/function.session-register.php