慕用7009936
2017-08-08 20:42
class Car {
private $ary = array();
public function __set($key, $val) {
$this->ary[$key] = $val;
}
public function __get($key) {
if (isset($this->ary[$key])) {
return $this->ary[$key];
}
return null;
}
public function __isset($key) {
if (isset($this->ary[$key])) {
return true;
}
return false;
}
public function __unset($key) {
unset($this->ary[$key]);
}
}
$car = new Car();
$car->name = '汽车'; //name属性动态创建并赋值
echo $car->name;
你的ary是私有属性,只能通过self等调用,不能用this调用
PHP进阶篇
181835 学习 · 2577 问题
相似问题