PHP手工调用魔术函数
魔术函数是不需要自己手工去调用的。这是PHP文档中的说明。问题来了。父类中有__get,__set,子类不重载就不能完成功能。所以,还是要手工调的。是否可以呢?
你运行一下下面的代码就可以了。
<?php
error_reporting(E_ALL);
class A {
public function __get($name){
return '0';
}
public function __set($name, $value) {
$this->$name=$value;
echo($this->$name.'</br>');
}
}
class B extends A {
public function __get($name){
return '1';
}
public function __set($name, $value) {
parent::__set($name, $value);
}
}
class E extends B {
}
$test =new B();
echo($test->c.'</br>');
$test->d=2;
$test1=new E();
$test1 =new B();
echo($test1->c.'</br>');
$test1->d=2;
?>
输出结果是:
1
2
1
2
这说明,手工调用魔术函数是可以的。
页:
[1]