|
Posted by Curt Zirzow on 12/07/05 07:36
On Tue, Dec 06, 2005 at 10:46:36PM -0500, Alan Pinstein wrote:
> >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:
>
> Hmmm... I am not sure I believe this.
>
> I understand how object refs work in PHP5. I understood how they
> worked in PHP4. Sadly I had to learn the hard way... coming from a C/C
> ++ background it was wierd, and not really useful, which is probably
> why the updated it so nicely in PHP5.
>
> If & was not intended to work on objects in PHP5, then it should fail
> at runtime. I doesn't, and in fact has specific and different
> behavior from assignment WITHOUT &.
My original statement was to show how the the php4 = &$o is
different. To simply the problem you have:
<?php
// sets object $o to instance of stcClass
$o = new stdClass;
// sets $c to point to the same instance as what $o is
// pointing to
$c = $o;
// we no longer need $o, but...
$o = null;
// the instance of stdClass no longer exists
var_dump($o); /* null */
var_dump($c); /* object(stdClass)#1 (0) { } */
// sets object $o to point to instance stdClass
$o = new stdClass;
// $b and $o are now referencing the variable that happens to
// reference an object
$b = &$o;
// object gets destroyed cause both $b and $o are the same vars
$o = null;
var_dump($o); /* null */
var_dump($b); /* null */
?>
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.
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.
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 */
?>
> 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.
Curt
--
cat .signature: No such file or directory
Navigation:
[Reply to this message]
|