PHP Magic Methods Usage summary

Source: Internet
Author: User
PHP (foreign name: HypertextPreprocessor, Chinese name: "Hypertext Preprocessor") is a common open source scripting language. The syntax absorbs the features of C language, Java, and Perl. it has a low entry threshold, is easy to learn, and is widely used. it is mainly applicable to the Web development field. PHP file suffix: php.

PHP (foreign name: Hypertext Preprocessor, Chinese name: "Hypertext Preprocessor") is a common open source scripting language. The syntax absorbs the features of C language, Java, and Perl. it has a low entry threshold, is easy to learn, and is widely used. it is mainly applicable to the Web development field. PHP file suffix: php.


This article mainly introduces how to use PHP magic methods and summarizes PHP magic methods. if you are interested, please refer to them.


The magic method is a special feature of PHP object-oriented. They are triggered under specific circumstances. they all start with Double underscores. you can think of them as hooks, using the pattern method, you can easily overload PHP object-oriented objects (Overloading is the dynamic creation of class attributes and methods ). There are many magic methods in pairs. The following lists all the current PHP mode methods.

1. _ construct ,__ destruct
_ Constuct is called when constructing an object;
_ Destruct is called to explicitly destroy an object or end the script;

2. _ get ,__ set
_ Set is called when an inaccessible or non-existent attribute is assigned a value.
_ Get is called when reading an inaccessible or non-existent attribute

3. _ isset ,__ unset
_ Isset is called when isset () or empty () is called for inaccessible or non-existent attributes
_ Unset is called when an unset operation is performed on an inaccessible or non-existent attribute.

4. _ call ,__ callStatic
_ Call is called when a method that is inaccessible or does not exist
_ CallStatic is called when a static method that is inaccessible or does not exist

5. _ sleep ,__ wakeup
_ Sleep is called when serialize is used. it is useful when you do not need to save all data of large objects.
_ Wakeup is called when unserialize is used. it can be used to initialize objects.

6. _ clone
Called to adjust the cloning behavior of an object.

7. _ toString
Called when a class is converted to a string

8. _ invoke
Called when an object is called in function mode

9. _ set_state
This static method is called when var_export () is called to export the class. Use the return value of _ set_state as the return value of var_export.

10. _ debuginfo
It is called when var_dump () is called to print an object (when you do not want to print all attributes) for PHP5.6

Source: Network

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.