Re: [PHP4BETA] Session: objects containing objects
| From: | Boris Erdmann | Date: | Thu, 24 Feb 2000 14:50:11 +0000 |
| Subject: | Re: [PHP4BETA] Session: objects containing objects | ||
| References: | 1 2 3 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-10850@lists.php.net to get a copy of this message | ||
Ulf Wendel wrote:
>
> thies@digicol.de wrote:
>
> To be able to write your own serialize with php you would need:
>
> getFields( Object $obj );
> getFielType ( Slot $slot );
> getConstructorCall ( Object $obj );
>
> - BUT - I'm not in favour to do so if the php4 serialize works perfect
> on objects. The major difference between the phplib serialize and the
> php4 serialize seems to be that php4 serializes all slots. phplib does
> not.
>
> To make it short: I'd replace the phplib serialize with the php4
> serialize.
Dear Ulf,
its very simple to make up a test script that answers your questions,
i think:
--------------------------------------------------
<?php
class bar {
var $bar_foo = "no success";
}
class foo {
var $foo_foo="foo";
var $foo_bar;
function foo() {
$this->foo_bar = new bar;
}
}
session_start();
if ( ! isset($obj) ) {
$obj = new foo();
$obj->foo_bar->bar_foo="success";
echo "please reload: ";
session_register("obj");
} else {
echo "<pre>\n";
echo "\$obj is of type: ".gettype($obj)."\n";
echo "\$obj is of class: ".get_class($obj)."\n";
echo "\n";
echo "\$obj->foo_foo: ".$obj->foo_foo."\n";
echo "\$obj->foo_bar->bar_foo: ".$obj->foo_bar->bar_foo."\n";
echo "\n";
}
?>
<a href="ulf.php4">klick</a>
-------------------------------------------------
This script saves a session with an object containing an object.
The first time this script is started it instantiates the object
$obj from class foo, which then contains an object $obj->foo_bar
from class bar. $obj->foo_bar then contains a slot "bar_foo" which
gets initialized to "no success". Just after instantiating $obj its
child object's slot gets changed to "success". This is the printout
when you restart the script by klicking "klick".
By doing so, you can see, that object serialization seems to work
for a level <= 2. Assuming that variable serialization is recursive
in php4 it is already prooven, that this works for all levels less than
any natural number :-)
Furthermore:
From the source of the example you can see: In addition to php3's
gettype() function php4 provides get_class(), which tells you the
class name of an object. (Showing one of the typical inconsistencies in
php: two merried functions with different syntax - yes i am talking
about the underscore)
Now: together with the ability of walking through an object via
reset($obj); while ( list($slot) = each($obj) ) { you_name_it; }
you have the three functions/methods you requested for
basic introspection.
Well, i hope that helps.
Boris