__set()方法用来设置私有属性
__get()方法用来获取私有属性
<?php
class Person
{
private $name;
private $age;
private $sex;
public function __set($name,$value) {
$allow = array("name","age");
if (in_array($name, $allow)) {
echo __METHOD__;
echo "<br>";
$this->$name = $value;
}
}
public function __get($name) {
echo __METHOD__;
echo "<br>";
return $this->$name;
}
}
$person = new Person();
$person->name = "gaobo";
$person->sex = 1;
echo $person->name;
?>