日期:2014-05-17  浏览次数:20410 次

php __get() __set() __construct()

function __construct($name, $sex, $age, $school)

??? ??? {

??? ??? ??? $this->name=$name;

??? ??? ??? $this->sex=$sex;

??? ??? ??? $this->age=$age;

??? ??? ??? $this->school=$school;

}

?

?

?

function __get($property_name)
??{?
??? //echo "在直接获取私有属性值的时候,自动调用了这个__get()方法<br>";
????if(isset($this->$property_name)) {
????return($this->$property_name);
??? }else {
????return(NULL);
???}
??}

??//__set()方法用来设置私有属性
??function __set($property_name, $value)
??{
??//?echo "在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值<br>";
???$this->$property_name = $value;
??} ?

?

?

function setAge($age)? //为外部提供一个公有设置年龄的方法

??? {

??? ??? if($age<0 || $age>130) //在给属性赋值的时候,为了避免非法值设置给属性

??? ??? ??? return;

??? ??? $this->age=$age;

}

function getAge() ??? //为外部提供一个公有获取年龄的方法

{

??? return($this->age);

}

?