<?phpheader("content-type:text/html;charset=UTF-8");classPerson{//私有的成员属性,对直接访问象private$name;private$age;private$sex;//魔术方法__construct(),__set(),__unset(),__isset(),__unset().....function__construct($name="name1",$age=20,$sex="女"){$this->name=$name;$this->age=$age;$this->sex=$sex;}/*输出CannotaccessprivatepropertyPerson::$name对象不能直接访问和设置私有属性的值,但是通过魔术方法__get($proName),__set($proName,$proValue)可以做到.步骤:1.重写魔术方法__get($property),__set($proName,$proValue)2.用对象直接访问或设置私有属性$p1->name="对象直接访问私有属性";echo$p1->name;3.对象直接访问或设置私有属性时,会自动调用魔法方法__get($proName),__set($proName,$proValue)*/function__get($proName){return$this->$proName;}function__set($proName,$proValue){$this->$proName=$proValue;}functionsay(){echo"$this->name:我的年龄$this->age,性别:$this->sex<br>";}functionrun(){$this->left();$this->right();}privatefunctionleft(){echo"left";}privatefunctionright(){echo"right";}//析构方法,对象销毁前自动调用function__destruct(){echo"$this->name:我走了<br>";}}$p1=newPerson("name1",15,"女");$p2=newPerson("name2",20,"男");$p3=newPerson("name3",30,"女");/*对象直接访问或设置私有属性*/$p1->name="对象直接访问私有属性";echo$p1->name."<br>";/*输出,注意__destruct()的输出顺序name1:我的年龄15,性别:女name2:我的年龄20,性别:男name3:我的年龄30,性别:女name1:我走了name3:我走了name2:我走了*/$p1->say();$p2->say();$p3->say();$p1=null;?>