执行如下代码:
<?php
class Test
{
private $property;
function __destruct()
{
echo "Destroying ".$this->$property."<br />";
}
function __set($name,$value)
{
$this->$name = $value;
}
}
$a = new Test();
$a->property = 5;
?>
在
__set()
方法中,你设置__set("a", 1)
,$name
作为局部变量,会转化为"a"
,$this->$name
等价于$this->a
,在
__destruct()
中, 局部变量$property
未定义,会有一个默认值""
,属性值不能为空字符串,所以报错。