The Magic method in PHP

Source: Internet
Author: User

Tag: Operation ISS Resource cannot start with recommended void error INF

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

If there is a system check out the Magic method will call itself.
Caution
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 __.

__sleep () and __wakeup ()

Public array __sleep (void)

void __wakeup (void)

The serialize () function checks to see if there is a magic method __sleep () in the class. If present, the method is called first before the serialization operation is performed. This feature can be used to clean up objects and return a
An array that contains all the variable names that should be serialized in the object. If the method does not return anything, NULL is serialized and a E_notice level error is generated.

Note:
__sleep () cannot return the name of the private member of the parent class. Doing so produces a e_notice level of error. Can be replaced with Serializable interface.
The __sleep () method is commonly used to commit uncommitted data, or similar cleanup operations. At the same time, if there are some very large objects, but do not need to save all, this function is very useful.
In contrast, unserialize () checks for the existence of a __wakeup () method. If present, the __wakeup method is called before the resource required by the object is prepared beforehand.
__wakeup () is often used in deserialization operations, such as re-establishing a database connection, or performing other initialization operations.

The Magic method in PHP

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.