Re: references, circular references, oop, and garbage collection in PHP5

From: Date: Thu, 08 Dec 2005 02:29:52 +0000
Subject: Re: references, circular references, oop, and garbage collection in PHP5
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-227101@lists.php.net to get a copy of this message
[ man.. I just realized that "reply" to this list doesn't send to the list... replies being re-sent... thanks for the help! ] On Dec 6, 2005, at 10:45 PM, Curt Zirzow wrote:
I'm going to jump to the code as fast as possible to explain what I can, the key thing to remember in php5 is that the old &$var declaration has no real meaning in objects. php5's objects exist outside of the old oop reference. Consider: class Object { public $val; function __construct($v) {
    $this->val = $v;
} } In PHP5: $o = new Object(2); $b = $o; $b->val = 3; print $o->val; // echos 3 in PHP4 (assuming var is used instead of public) $o = new Object(2); $b = $o; $b->val = 3; print $o->val; // echos 2 This is cause objects in php5 exist all on there own, that get referenced to a php variable. On Tue, Dec 06, 2005 at 05:23:45PM -0500, Alan Pinstein wrote:
Question #1: Is the fact that references to objects in the form $objRef = &$obj don't bump the refcount of $obj an intended behavior that can be counted on? If so, cool! So, now that we have a way to do weak references, we should be able to implement a reasonable memory management scheme for parent-child objects. Normally from the client side the interface should look something like: $parent = new Parent(); $child = new Child(); $parent->addChild($child);
Ok, i get to the code and well what I mentioned above explains why there is no need to use the $o = &$object; If I take your code and run it against one of the latest version's of php 5.1 i will get a var_dump($child) of: object(pChild)#2 (1) { ["parent"]=> object(pParent)#1 (1) {
    ["children"]=>
    array(1) {
      [0]=>
      object(pChild)#2 (1) {
        ["parent"]=>
        object(pParent)#1 (1) {
          ["children"]=>
          array(1) {
            [0]=>
            *RECURSION*
          }
        }
      }
    }
} } Which is what I see as expected results. Curt. --cat .signature: No such file or directory --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#227101) next »