PHP _get () _set () _construct ()

Source: Internet
Author: User
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 "Automatically calls this __get () method when it directly acquires the value of a private property.
";
???? if (Isset ($this-$property _name)) {
???? Return ($this, $property _name);
??? }else {
???? return (NULL);
???}
??}

?? The __set () method is used to set the private property
?? function __set ($property _name, $value)
?? {
?? "echo" Automatically calls this __set () method to assign a value to a private property when setting the value of a private property directly
";
??? $this $property _name = $value;
??} ?

?

?

function Setage ($age)? provide an external method for setting the age of a public

??? {

??? ??? if ($age <0 | | $age >130)// when assigning a value to a property, in order to avoid an illegal value set to the property

??? ??? ??? Return

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

}

function Getage () ??? to provide an external public access to the age method

{

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

}

?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.