Home  >  Article  >  Backend Development  >  php _get() _set() _construct()

php _get() _set() _construct()

WBOY
WBOYOriginal
2016-06-13 11:03:06976browse

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($age130) //在给属性赋值的时候,为了避免非法值设置给属性

??? ??? ??? return;

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

}

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

{

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

}

?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn