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

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

 




On Dec 7, 2005, at 12:36 AM, Curt Zirzow wrote:

My original statement was to show how the the php4 = &$o is
different.

Oh, well, sure I believe that! :)

In php5 variables are just containers that point to objects, so when you make
a variable a reference to another variable all you are doing is
saying these variables are the same thing.


That's good to know... one thing that's difficult about PHP is that everything's opaque and not well documented. In C/C++ I know what things actually are b/c you can see the typedefs. In php I just don't know how objects are represented, what references are, etc.

So, knowing that an "object" variable is really just a "special container object" that points to the real instance, this is useful. That isn't clear from the php docs. Thanks!

php5's objects dont know any such thing as a reference, they just
know of instances of themselves. The variables ($o, $a, $b)
existance is just a container for the instance of the object. So in
the case when I do a:

  $b = &$o;

All that is happening is the container is identical, so when I say:

  $o = null;

Since $b is the same thing as $o , $b is set to null as well and
thus, there are now more variable (containers) that reference to
the instance of the  object, thus the object will get destroyed,
but.. if i say we have two containers:

  $o = new stcClass;
  $b = $o;

Now the instance of that 'new StdClass' is contained in two vars,
when I set $o to null, $b still exists since it doesn't know about
$o whats so ever, and the instance of the stdClass still exists.


Yes, this makes sense, too. It's tricky, with so many levels of indirection. So at the core level, you've got the "real" object instance. Then, you've got N container "object variables" that point to the real instance. PHP refcounts the number of object variables pointing at the real instance. Then, on top of that, you have references, which are "aliases" to "object variables" and thus don't affect the ref count. So good this all makes sense and agrees with the behavior I see.

I guess it comes down to objects are treated the same way as you
would expect these results:

<?php

  $i = 1;  /* aka new object */
  $k = $i;

  $i =  null;
  var_dump($i); /* null */
  var_dump($k); /* int(1) */

  $i = 1;  /* aka new object */
  $j = &$i;

  $i = null;
  var_dump($i); /* null */
  var_dump($j); /* null */


Yes...

The sample code below shows that indeed, in practice, on 5.0.4, that
& will create another reference (ie a weak reference) to an object
WITHOUT incrementing the refcount....

I'm not sure how you mean a weak reference, and well a refcount is
rather meaning less in php userland.

So, this gets interesting. I don't know if you're familiar with the circular-reference problem. But if you have two instances that have references to each other, even once you remove all references to the objects, they will not be GC'd since they have a mutual deadlock on each other:

$a = new MyObj;
$b = new MyObj;
$a->setB($b);		// does $this->b = $b;
$b->setA($a);		// does $this->a = $a;

$a = NULL;
$b = NULL;

The actual instances pointed to by $a and $b WILL NOT GET FREED HERE as you would *wish*. However this is expected behavior.

Only by changing MyObj to store "weak references", that is references to the objects that are NOT reference-counted, can you get the GC to free the instances.

function setB(&$B) { $this->b = &$a; }
function setA(&$B) { $this->a = &$b; }

Now, the instances will be freed when the $a and $b are null'd out.

So, while I now feel more confident of how references act with respect to objects (which is, they act the same as they do for any variable), I still am not sure what "$this->this" is and why it worked so magically.

Thanks for the explanations! I feel better about this now.

Alan

--
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