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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux