PHP Magic Method Summary

Source: Internet
Author: User
Tags modifier php error serialization valid

Summary of Magic Methods in PHP: __construct, __destruct, __call, __callstatic,__get, __set, __isset, __unset, __sleep, __wakeup, __toString , __set_state, __clone and __autoload

1, __get, __set

These two methods are designed for properties that are not declared in the class and their parent class.

__get ($property) Access this method when an undefined property is invoked

__set ($property, $value) is called when assigning a value to an undefined property.

No declarations here include properties that have access control of Proteced,private when using object invocation (that is, properties that do not have permission to access)

2, __isset, __unset

__isset ($property) This method is called when the Isset () function is invoked on an undefined property

__unset ($property) This method is called when the unset () function is invoked on an undefined property

In the same way as the __get method and the __set method, there are no declarations including properties that have access control as proteced,private when using object invocation (that is, properties that do not have permission to access)

3, __call

__call ($method, $arg _array) when calling an undefined method is to call this Shantiu

Undefined methods here include methods that do not have permission to access

4, __autoload

The __autoload function, which is invoked automatically when an attempt is made to use a class that has not been defined. By calling this function, the script engine had the last chance to load the class that was required before the PHP error failed.

Note: Exceptions thrown in the __autoload function cannot be caught by a catch statement block and cause a fatal error. 5, __construct, __destruct

__construct constructs a method that calls this method when an object is created, and the advantage of using this method is that you can make the construction method have a unique name, regardless of the name of the class in which it resides. So you're changing the name of the class, you don't need to change the name of the constructor method

__destruct destructor, PHP calls this method before the object is destroyed (that is, before it clears out of memory)

By default, PHP only frees the memory occupied by object properties and destroys object-related resources.

Destructors allow you to clean up memory by executing arbitrary code after using an object.

Destructors are invoked when PHP determines that your script is no longer related to objects.

Within the namespace of a function, this occurs when the function return.

For global variables, this happens at the end of the script. If you want to explicitly destroy an object, you can assign any other value to the variable that points to the object. The variable is usually assigned on duty as null or the unset is invoked.

6, __clone

An object assignment in PHP5 is a reference assignment that is used, and if you want to copy an object, you need to use the Clone method, and calling this method is an object that automatically invokes the __clone magic method

If you need to perform some initialization operations on object replication, you can implement the __clone method in the

7, __tostring

The __tostring method is invoked automatically when an object is converted to a string, such as when the object is printed with the Echo

If the class does not implement this method, the object cannot be printed through ECHO, or it will display: Catchable fatal Error:object of class test could not is converted to string in

This method must return a string before PHP 5.2.0, the __tostring method takes effect only if echo () or print () is used in conjunction. After PHP 5.2.0, it can take effect in any string environment (for example, by printf (), using the%s modifier) but not in a non-string environment (such as using the%d modifier). From PHP 5.2.0, if you convert an object that does not define a __tostring method to a string, a e_recoverable_error error is reported.

8, __sleep, __wakeup

When __sleep is serialized, use

__wakeup Drag when the row is called

Serialize () checks whether the class has a magic name __sleep function. If so, the function will run before any serialization. It can clear the object and should return an array that contains all the variable names that should be serialized in the object.

The purpose of using __sleep is to turn off any database connections that the object might have, commit the waiting data, or perform similar cleanup tasks. Also, this function is useful if you have a very large object and you do not need to store it completely.

Conversely, unserialize () checks the existence of a function that has a magic name __wakeup. If present, this function can reconstruct any resources that the object might have.

The purpose of using __wakeup is to reconstruct any database connections that may be lost in serialization and to handle other reinitialization tasks.

9, __set_state

When Var_export () is invoked, this static method is invoked (valid from PHP 5.1.0).

The unique argument for this method is an array that contains a press array (' property ' => value, ...) The class attribute of the format arrangement.

10, __invoke

The __invoke method is invoked automatically when an attempt is made to call an object in the form of a call to a function.

PHP5.3.0 above version is valid

11, __callstatic

It works like the __call () Magic Method, __callstatic () to handle static method calls.

PHP5.3.0 above version is valid

PHP does strengthen the definition of the __callstatic () method, it must be public and must be declared static. Similarly, the __call () Magic method must be defined as public, and all other magic methods must be so.

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.