The Magic method of Php Magic method in PHP class

Source: Internet
Author: User
Tags php class
Defined:

The method that begins with the two line "__" in the PHP class is called the Magic method.

Classification:

For example: Construction method: __construct; destructor: __destruct; dynamic reload: __set (), __get (), __call (), __callstatic (), Clone of object: __clone ()

Application Scenarios:

1. When the program attempts to modify a non-existent or invisible class property, the PHP engine will call the __set () method, if the method is defined in the class. __set () is defined in the following format:

function __set ($name, $value) {

Implementation Details

}

Where $name is a dynamically created variable name, $value is the value of the variable.

2. When the program attempts to read a non-existent or invisible class attribute, the PHP engine will call the __get () method, if the method is defined in the class. __get () is defined in the following format:

function __get ($name) {

Implementation Details

}

Where $name are dynamically created variable names.

3. When the program tries to invoke a non-existent or invisible class method, the PHP engine will call the __call () method, if the method is defined in the class. The __call () method is defined in the following format:

function __call ($name, $args) {

Implementation Details

}

Where $name is the dynamically created method name, $args is the parameter of the method, in the form of an array.

4, starting from PHP5.3.0, you can use __callstatic () to dynamically create static methods. __callstatic () is defined in the following format:

function __callstatic ($name, $args) {

Implementation Details

}

Where $name is the dynamically created method name, $args is the parameter of the method, in the form of an array.

5. Once the __clone () method is defined, the class is automatically called when it is copied, so that we can re-open the memory to the reference property in the __clone () method. The __clone () definition format is as follows:

function __clone () {

Implementation Details

}

The above describes the PHP magic method in the PHP class magic method, including the PHP magic method of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.