php:关于间接调用__set和__get


   
  <?php
  
class classname
{
public $attribute;
function __get($name)
{
echo "This is get function<br />";
return $this->$name;
}
function __set($name,$value)
{
echo "This is set function<br />";
$this->$name=$value;
}
}

$a=new classname();
//测试1
$a->attribute="demo";
echo $a->attribute;

//测试2
//$a->__set("attribute","demo");
//echo $a->__get("attribute")."<br />";
?>

使用测试1时,输出结果

使用测试2时,输出结果

   
  This is set function
  
This is get function
demo

书上说调用属性时系统会自动调用__set和__get方法,但是我测试完发现也没调啊,是书上骗人还是我理解的有问题呢

php

伊尔明斯特 10 years, 4 months ago

public $attribute;
改为:
private $attribute;

此处前方是荒野 answered 10 years, 4 months ago

Your Answer