<?php
class Person{
private $name;
private $sex;
private $age;
private $addr;
function __construct($name="", $sex="male", $age=0, $addr=""){
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
$this->addr=$addr;
}
function __destruct(){
//echo "Exit successfully.";
}
//magic method __set()
function __set($property,$value){
if($property=="sex"){
if(!($value=="male" || $value=="female")){
return;
}
$this->$property=$value;
}
if($property=="age"){
if($value>150 || $value<0){
//die("Unsuitable age value.<br/>");
return;
}
$this->$property=$value;
}
}
function __get($property){
if($property=="sex"){
return "It's a secret.<br/>";
}
}
function run(){
echo $this->name." is running.".$this->leftLeg()." and then ".$this->rightLeg().".<br/>";
}
private function leftLeg(){
return "move left leg";
}
private function rightLeg(){
return "move right leg";
}
function say(){
echo "I am".$this->name.", ",$this->age." years old, ".$this->sex.". And I live in ".$this->addr."<br/>";
}
}
$Lee=new Person("Bruce Lee","female",23,"ShangHai");
$Lee->age=250;
$Lee->sex="male";
$Lee->say();
echo "sex: ".$Lee->sex;