PHP Magic Method

Source: Internet
Author: User

Magic Method: Two The formatting that begins with an underscore.

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

__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 called (get)
__set ($property, $value) called when assigning a value to an undefined property (set)
No declarations are included when using object invocation, access control is a property of Proteced,private (that is, properties that do not have permission access)

__isset, __unset
__isset ($property) is called when the isset () or empty () function is called on an inaccessible property.                            __unset ($property) called when the unset () function is called on an inaccessible property. As with the __get method and the __set method, there is no declaration here that the access control is a property of Proteced,private when using an object invocation (that is, a property without permission access)
③__call, __callstatic
__call ($method, $arg _array) is called when an undefined method is called (handles a non-static method). Undefined methods here include methods that do not have permission access
__callstatic () is called when an undefined method is called with a static method. __callstatic () is to handle static method calls,
PHP5.3.0 above version 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 do so.

__autoload
__autoload function, It is called automatically when you try to use a class that is not already defined. By calling this function, the scripting engine has the last chance to load the required classes before PHP fails.
Note: Exceptions thrown in the __autoload function cannot be caught by a catch statement block and cause a fatal error.

__construct, __destruct
__construct construction method, The advantage of using this method when an object is created is that it can have a unique name for the constructor, regardless of the name of the class in which it resides. So that when you change the name of a class, you do not need to change the name of the constructor
__destruct destructor method, PHP calls this method before the object is destroyed (that is, before it is purged from memory)
By default, PHP simply frees the memory occupied by the object's properties and destroys the object-related resources.
Destructors allow you to execute arbitrary code after an object is used to clear memory.
when PHP determines that your script is no longer related to the object, the destructor is called.
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. You typically assign a variable to be on duty to null or call unset.

__clone
PHP5 Object Assignment is the reference assignment used. If you want to copy an object then you need to use the Clone method, which is called by the object to call the __clone Magic method automatically
if you need to perform some initialization on object replication, you can implement

__tostring
__tostring method is called automatically when an object is converted to a string, such as when using Echo to print an object
if the class does not implement this method, The object cannot be printed via echo, otherwise it will be displayed: Catchable fatal Error:object of class test could not being converted to string in
This method must return a string

Before PHP 5.2.0, the __tostring method will only take effect if you use Echo () or print () together. After PHP 5.2.0, you can take effect in any string environment (for example, with the%s modifier through printf (), but not in a non-string environment (such as using the%d modifier). From PHP 5.2.0, a e_recoverable_error error is reported if an object with an undefined __tostring method is converted to a string.

⑧__Sleep, __wakeup
__sleep serialization (used to clean up data)
__wakeup crossdress Call (Initialize operation, deserialization operation, re-establish database connection)
Serialize () checks the class for a function that has the magic name __sleep. If so, the function will run before any serialization. It clears the object and should return an array containing all the variable names that should be serialized in the object.
The purpose of using __sleep is to close any database connections that an object might have, submit data for waiting, or perform similar cleanup tasks. In addition, this function is useful if you have very large objects and do not need to be fully stored.
Conversely, unserialize () checks for the existence of a function with the magic name __wakeup. If present, this function can reconstruct any resources that an 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.

__set_state
This static method is called when Var_export () is called (Valid from PHP 5.1.0).
The only parameter to this method is an array that contains the by array (' property ' = = value, ...) The class properties of the format arrangement.

__invoke
The __invoke method is called automatically when an attempt is made to invoke an object in a way that invokes a function.
PHP5.3.0 above version Valid

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.