PHP Common Magic Method Detailed _php Example

Source: Internet
Author: User
Tags modifier php error serialization

PHP rules that start with two underscores (__) are reserved as magic methods, so it is recommended that you do not start with a function name unless you are trying to overload an existing magic method.

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

1, __get, __set

These two methods are designed for properties that are not declared in the class and their parent class.
__get ($property) When an undefined property is invoked, this method is triggered and the passed parameter is the name of the property being accessed
__set ($property, $value) when assigning a value to an undefined property, this method is triggered and the passed parameter is the property name and value that is set
The absence of a declaration here includes a property (that is, a property that has no access) when an object invocation is used, and the access control is proteced,private.

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 calling this method
Undefined methods here include methods that do not have access to permissions; If the method does not exist to find this method in the parent class, if the parent class does not exist to call the __call () method of this class, if there is no __call () method in this class to find the __call () method in the parent class

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.
If you want to define a global automatic load class, you must register the processing class with the Spl_autoload_register () method in the PHP standard library:

Copy Code code as follows:

<?php
Class Loader
{
static function Autoload_class ($class _name)
{
Look for the correct $class_name class, and introduce that no exception is thrown
}
}
/**
* Set automatic loading of objects
* Spl_autoload_register-register given function as __autoload () implementation
*/
Spl_autoload_register (Array (' Loader ', ' Autoload_class '));
$a = new Test ();//test require on instantiation, automatic loading, many frameworks use this method to automatically load classes
?>

Note: The exception thrown in the __autoload function cannot be caught by a catch statement block and can cause a fatal error, so it should be caught in the function itself.

5, __construct, __destruct

__construct constructs a method that calls this method when an object is created, and the benefit of using this method relative to PHP4 is that you can make the construction method have a unique name, regardless of the name of the class in which it resides. So when you change the name of a class, you don't need to change the name of the constructor
__destruct destructor, PHP calls this method before the object is destroyed (that is, before it is purged from memory). By default, PHP releases only the memory occupied by the object's properties and destroys the object-related resources, and the destructor allows you to execute arbitrary code to erase memory 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 occurs 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, which you can implement in the __clone method if you need to perform some initialization operations on the object copy.

7, __tostring

The __tostring method is invoked automatically when an object is converted to a string, such as when the object is printed with 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.