From PHP 5 onwards, classes in PHP can use the Magic method. The rules that begin with two underscores (__) are preserved as magic methods, so it is recommended that you do not start with the function name unless you are trying to reload the existing magic method. 1, __get, __set These two methods are designed for properties that are not declared in classes and their parent classes. __get ($property) When an undefined property is called, this method is triggered, and the passed parameter is the property name that is 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. There is no declaration here that the access control is a Proteced,private property (that is, a property without permission access) when using object invocation. 2, __isset, __unset __isset ($property) This method is called when the Isset () function is called on an undefined property. __unset ($property) This method is called when the unset () function is called on an undefined 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 that does not have permission to access). 3. __call __call ($method, $arg _array) This method is called when an undefined method is called. The undefined method here includes a method that does not have permission to access, if the method does not exist in the parent class to find this method, 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 __autoload function, which will be called automatically when trying to use a class that has not yet been defined. By calling this function, the scripting engine has the last chance to load the required classes before PHP fails. If you are defining a global auto-load class, you must register the processing class with the PHP standard library using the Spl_autoload_register () method: [Php]class Loader {static function Autoload_class ($ class_name) {//Find the correct $class_name class and introduce, no then throw an exception}}/** * Set the object's Auto-load * spl_autoload_register-registerGiven function as __autoload () implementation */Spl_autoload_register (Array (' Loader ', ' Autoload_class ')); $a = new Test ();//test does not use require to instantiate, implement automatic loading, many frameworks use this method to automatically load class class Loader {static function Autoload_class ($class _name) {/ /Look for the correct $class_name class and introduce, no then Throw exception}}/** * Set object's Auto load * spl_autoload_register-register given function as __autoload () imple mentation */spl_autoload_register (Array (' Loader ', ' Autoload_class ')); $a = new Test ();//test does not use require to instantiate, automatic loading, many frameworks use this method to automatically load the class Note: The exception thrown in the __autoload function cannot be caught by a catch statement block and causes a fatal error, so it should be captured in the function itself. 5, __construct, __destruct __construct construction method, when an object is created to call this method, the advantage of using this method with respect to PHP4 is: You can make the constructor method have a unique name, Regardless of the name of the class in which it resides. So you don't need to change the name of the constructor when you change the name of the class. __destruct destructor, 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, and the destructor allows you to execute arbitrary code to clear the memory after using an object. When PHP decides that your script is no longer related to the object, the destructor is called. Within the namespace of a function, this occurs at the time of 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. 6, __clone the object assignment in PHP 5 is to use the reference assignment, if you want to copy an object you need to use the Clone method, call this method is an object will automatically call __CLOne magic method can be implemented in the __clone method if the object replication requires certain initialization operations to be performed. 7. The __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 through 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 character 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. 8, __sleep, __wakeup __sleep serialization when using __wakeup crossdress when the call serialize () check the class for Magic name __sleep function. 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. 9, __set_state when calling Var_export (), this static method 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. 10, __invoke (PHP 5.3.0 or later) when an attempt is made to invoke an object in a way that invokes a function, the __invoke method is called automatically. 11, __callstAtic (PHP 5.3.0 or later) it works like The __call () Magic Method, __callstatic () is to handle static method calls. 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
http://www.bkjia.com/PHPjc/477768.html www.bkjia.com true http://www.bkjia.com/PHPjc/477768.html techarticle from PHP 5 onwards, classes in PHP can use the Magic method. Its provisions to two underscore (__) The method that starts with the Magic method, so we recommend that the function name is best not to ...