PHP Magic Method Learning Notes

Source: Internet
Author: User

__get () __set () __isset () __unset

__call __callstatic

__call is called automatically when a method is called that is not visible (does not exist or does not have permissions).

The Say method __call ("Say", Array (->say)) does not exist in the $lisi.

__get () is used to get the property value of a private member, with a parameter that passes in the property name of the private member you want to get, and returns the obtained value. This method does not need to be called by hand, it only needs to be encapsulated in the class. If the member property is not encapsulated as private, the object itself does not automatically call this method. The following code

The __get () method is used to get the private property

Class tian{
Private $age = 18;
Public Function __get ($property _name)
{
if (Isset ($this-$property _name)) {
Return ($this, $property _name);
}

}
}
$a =new Tian ();
Echo $a->age;

If the private property to be called exists, the value passed to the private property is returned, otherwise it is empty $a=new Tian (); the $a->age; here gets 18, through the __get () Magic method, so that the private property can be called outside the class. __set () assigns a value to a private property, has two parameters, one parameter is the name of the private property, the second parameter is the value to assign to the private property, there is no return value, nor does it need to be called manually, and is encapsulated in the class. For example, the following code

Class tian{
Private $age = 18;
Public Function __get ($property _name)
{
if (Isset ($this-$property _name)) {
Return ($this, $property _name);
}

}

Public function __set ($a, $v) {
echo $this $a;

}
}
$a =new Tian ();
echo $a->name= "big idiot";

If the member property is not private, it is not called to the function.

PHP Magic Method Learning Notes

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.