今天碰到一个关于php变量内存分配的问题,记录一下。
如下这段代码:
复制代码 代码如下:
$a = array (
'str' => 1,
'child' => 2
);
$b = $a;
$b['child'] = $a;
$b['child']['str'] = 2;
echo $b['str'];
$b = null;
echo $a['str'];
复制代码 代码如下:
class A
{
public $str = '';
public $child;
}
$a = new A();
$b = $a;
$a->str = 1;
$a->child = 2;
$b->child = $a;
$b->child->str = 2;
echo $b->str;
$b = null;
echo $a->str;