PHP Magic Method

Source: Internet
Author: User

1,__construct (), __destruct (), __call (), __callstatic (), __get (), __set (), __isset (), __unset (), __sleep (), __ Methods such as wakeup (), __tostring (), __invoke (), __set_state (), __clone (), and __debuginfo () are called " Magic Methods" (Magic methods) in PHP. You cannot use these method names when naming your own class methods, unless you want to use their magic features.

2. Precautions

PHP retains all class methods that begin with __ (two underscores) as a magic method. Therefore, in addition to the above magic method when defining a class method, it is recommended that you do not prefix __.

3, __get and __set

__get:
__get Magic method, which is automatically called when external access to properties in the class cannot be directly accessed
The Magic method has its default definition format and cannot be changed arbitrarily.
The Magic method cannot be added static modifier, out of the callstatic
__get Magic method must have a return value

__set:
__set Magic Method, which is called automatically when assigning a value to a property that cannot be directly accessed in a class
Property_exists  detects whether a class or object contains a property of the specified name

4 , __call Magic Method:
When an inaccessible method is called in an object, __call () is called.
Method_exists A function method that determines whether a class contains a specified name





PHP Magic Method

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.