Home > Backend Development > PHP Tutorial > php _get() _set() _construct()

php _get() _set() _construct()

WBOY
Release: 2016-06-13 13:03:02
Original
1049 people have browsed it

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);

}

?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template