PHP头条
热点:

php_get()_set()_construct()


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()方法
";
????if(isset($this->$property_name)) {
????return($this->$property_name);
??? }else {
????return(NULL);
???}
??}

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

?

?

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

??? {

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

??? ??? ??? return;

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

}

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

{

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

}

?

www.phpzy.comtrue/phprm/31470.htmlTechArticlephp_get()_set()_construct() php __get() __set() __construct() function __construct($name , $sex , $age , $school ) ??? ??? { ??? ??? ??? $this->name=$name; ??? ??? ??? $this->sex=$sex; ??? ??? ??? $this->age=$age; ??? ??? ??? $this->school=...

相关文章

PHP之友评论

今天推荐