【发布时间】:2012-06-13 15:10:44
【问题描述】:
<?php
class SimpleClass
{
public $var1;
}
$instance = new SimpleClass();
$assigned = $instance;
$reference =& $instance;
$instance->var1 = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
exit;
?>
有人可以帮忙吗?上面代码的输出怎么来的:
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
对于$instance 和$reference,我可以理解NULL,但是为什么$assigned 没有变成NULL。
根据我在 PHP 5 中的理解,对象是通过引用传递的,所以 $assigned 也包含引用,在这种情况下它也应该成为 NULL。
除了我的理解之外,PHP手册中写的是“当将一个已经创建的类实例分配给一个新变量时,新变量将访问与分配的对象相同的实例。这种行为是相同的将实例传递给函数。"
谁能解释一下?
【问题讨论】:
标签: reference php-5.3 variable-assignment instantiation