2

I have following stdClass object and its assignment using reference object. But once after unsetting unset($A); still $B outputs the previous values of $A even a New value is assigned to $A's ->foo property. See the trace below.

<?php
$A = new stdclass;
$A->foo = 'AAA';
echo "Ouput $ A:";
echo "<pre>";
print_r($A);

/*
stdClass Object
(
    [foo] => AAA
)
*/

$B = &$A;
unset($A);

$A = new stdclass;
$A->foo = 'aaa';
echo "after unset $ A and again assigning new value. Ouput $ A:";
echo "<pre>";
print_r($A);
/*   prints like
stdClass Object
(
    [foo] => aaa
)
*/

echo "Ouput $ B:";
echo "<pre>";
print_r($B);
/*  prints like 
stdClass Object
(
    [foo] => AAA
)
*/

Edit:

Question is that $B was assigned a reference of $A but after unset of $A

  1. How it can print values of Previously assigned value of $A?
  2. If $A is unset and if $B is going to print value of $A then it should print new value of $A?

As we know in case of Shadow Copy if source object is vanished/destroyed then a reference object cannot point to a location where source object was pointing.

Smile
  • 2,770
  • 4
  • 35
  • 57

2 Answers2

2

Rather than saying unset($A), set it to NULL. Unset has so many different behaviors in different cases, its manual is worth a look.

$A=NULL;
//unset($A);

Fiddle

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • So here, How could we differentiate `$A=NULL;` and `unset($A);`? – Smile Oct 04 '13 at 07:04
  • NULL just gets the job done right away whereas unset checks for many different conditions and unsets differently based on different situations. Have a look at it's referenced manual – Hanky Panky Oct 04 '13 at 07:05
  • @NullVoid `$A=null` will assign `null` to `$A` (instantly removes its previous value, it'll affect also references). `unset($A)` will just mark variable as `deleted` but will not remove its value (it'll be inaccessible but will still be in memory until GC will delete it). – Elon Than Oct 04 '13 at 07:06
  • Also `unset` will not affect references. – Elon Than Oct 04 '13 at 07:08
  • So it does means that it is better to set `NULL` in this type of situation rather than `unset`ting it, right? – Smile Oct 04 '13 at 07:11
  • Unsetting a variable that's referenced somewhere else too might not unset it completely and hence unset in this case may not be used – Hanky Panky Oct 04 '13 at 07:15
1

How it can print values of Previously assigned value of $A?

The object that is identified by $A is referenced by $B. That's why unset($A) cannot garbage collect the object.

If $A is unset and if $B is going to print value of $A then it should print new value of $A?

No, unset breaks the connection between $A and $B. After that, $A = new stdclass creates a new variable (that happens to have the same name).

Oswald
  • 31,254
  • 3
  • 43
  • 68
  • So can we say that `Memory` were occupied for both objects `$A` and `$B` differently? Because this is not a case of `Shadow Copy`. – Smile Oct 04 '13 at 07:14
  • `unset` doesn't break connection between variables. But you can say that it break connection to place in memory to which both variables was pointing. – Elon Than Oct 04 '13 at 07:15
  • @ElonThan Ok. So Can I conclude that both objects were pointing to same location. But by `unset($A);` only `$A` has broken connection to location where it was pointing previously and `$B` is still pointing to same location referenced by `$A', right? – Smile Oct 04 '13 at 07:18
  • `$A` and `$B` contain object identifiers, not objects. Whether an object is destroyed depends on whether a variable exists, that holds the object identifier of the object. – Oswald Oct 04 '13 at 07:18