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.

4

2 回答 2

2

而不是说unset($A),设置它NULL。Unset 在不同的情况下有很多不同的行为,它的手册值得一看。

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

小提琴

于 2013-10-04T07:02:07.963 回答
1

它如何打印先前分配的 $A 值的值?

由 标识的对象由$A引用$B。这就是为什么unset($A)不能垃圾收集对象的原因。

如果 $A 未设置并且 $B 将打印 $A 的值,那么它应该打印 $A 的新值?

不,断开和unset之间的连接。之后,创建一个新变量(恰好具有相同的名称)。$A$B$A = new stdclass

于 2013-10-04T07:12:14.347 回答