正确姿势:
final class private_{ private $name='adminmas'; private $sex ='20'; public function __get($name){ return $this->$name; } public function __set($name ,$value ){ $this->name = $value; } } $cl = new private_; $cl->name = 'jun'; var_dump( $cl->name );
错误姿势:
final class private_{ static private $name='adminmas'; private $sex ='20'; public function __get($name){ return $this->$name; } public function __set($name ,$value ){ $this->name = $value; } } // $cl = new private_; private_::$name = 'jun'; var_dump( private_::$name );
总结:
__get() And __set() 魔术函数无法获得静态方式访问的属性名
必须得通过新建对象的方式访问.才能设置或获取和设置私有的属性.
ps:
PHP的高级特性:偶然间发现的.
class na{ static private $name= 'adminmas'; //self::$name //正确 public function __get($name){ return self::$name; //正确 } } $cl = new na; var_dump( $cl->name );