Php magic methods are summarized from versions later than php5, and php classes can use magic methods. Php requires that all methods starting with two underscores (_) are retained as magic methods. Therefore, we recommend that you do not start with _ in the function name unless you want to reload the existing magic methods. The magic methods in PHP are: __construct ,__ de php magic method summary
Php classes can use magic methods in versions later than php5.
Php requires that all methods starting with two underscores (_) are retained as magic methods. Therefore, we recommend that you do not start with _ in the function name unless you want to reload the existing magic methods. The magic methods in PHP are: __construct, _ destruct, _ call, _ callStatic, __get, _ set, _ isset, _ unset, _ sleep, _ wakeup, _ toString, _ set_state, _ clone, the _ autoload1, _ get, and _ set methods are designed for classes and their parent classes without declared attributes. _ get ($ property) this method is triggered when an undefined property is called. when the Accessed property name_set ($ property, $ value) is assigned a value for an undefined property, this method will be triggered. the passed parameter is the set attribute name and value. there is no declaration here, including when using an object call, the access control is proteced, private attributes (that is, attributes with no access permission ). 2. _ isset, _ unset _ isset ($ property) this method is called when the isset () function is called on an undefined property _ unset ($ property) when the unset () function is called on an undefined property, this method is the same as the _ get method and the _ set method. no declaration here includes when an object is called, access control is proteced and private (that is, attributes with no access permission) 3. _ call ($ method, $ arg_array) when calling an undefined method is to call this method, the undefined method here includes methods without access permission. if the method does not exist, find this method in the parent class, if the parent class does not exist, call the _ call () side of the class? If the _ call () method does not exist in this class, find the _ call () method in the parent class.
4. The _ autoload function is automatically called when attempting to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails. To define a global automatic loading class, you must use the spl_autoload_register () method to register the processing class to the PHP Standard Library:
?
View plaincopy to clipboardprint?
-
- Class Loader ??
- {??
- Static function autoload_class ($ class_name )??
- {??
- // Find the correct $ class_name class and introduce it. if not, an exception is thrown ??
- }??
- }??
- ??
- /**
- *?? Set automatic object loading
- *?? Spl_autoload_register-Register given function as _ autoload () implementation
- */??
- Spl_autoload_register (array ('loader ', 'autoload _ class '));??
- ??
- $ A = new Test (); // Test is instantiated without using require to implement automatic loading. many frameworks use this method to automatically load classes ??
- ??
- ?> ??
Note: the exceptions thrown in the _ autoload function cannot be caught by the catch statement block and cause fatal errors. Therefore, the function itself should be captured. 5. The _ construct and _ destruct _ construct constructor call this method when an object is created. the advantage of using this method relative to PHP4 is: the constructor has a unique name, no matter what the name of the class is. in this way, when you change the class name, you do not need to change the name_destruct destructor of the constructor. PHP will remove the constructor before the object is destroyed) call this method. By default, PHP only releases the memory occupied by object properties and destroys object-related resources. The Destructor allows you to execute arbitrary code after using an object to clear the memory. When PHP decides that your script is no longer related to objects, the destructor will be called. In the namespace of a function, this occurs when the function returns. 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 pointing to the object. usually, the variable value is NULL or the object value assignment in _ clonePHP5 called unset.6 is a reference value assignment. if you want to copy an object, you need to use the clone method, when this method is called, the object will automatically call the _ clone magic method. if you need to perform some initialization operations in object replication, you can implement it in the _ clone method. 7. The _ toString method is automatically called when an object is converted into a string, for example, when an object is printed using echo. If the class does not implement this method, the Object cannot be printed using echo; otherwise, the system will display: Catchable fatal error: Object of class test cocould not be converted to string in. this method must return a string. Before PHP 5.2.0, the __tostring method takes effect only when echo () or print () is used in combination. PHP 5.2.0 and later can take effect in any string environment (for example, using the % s modifier through printf (), but cannot be used in non-string environment (for example, using the % d modifier ). From PHP 5.2.0, if an undefined _ toString method object is converted to a string, an E_RECOVERABLE_ERROR error is returned. 8. When _ sleep and _ wakeup _ sleep are serialized, use _ wakeup to call serialize () check whether there is a magic name__ sleep function in the class. In this case, the function will run before any serialization. It can clear the object and should return an array containing all variable names to be serialized in the object. The purpose of _ sleep is to close any database connection that an object may have, submit data in waiting or perform similar cleanup tasks. In addition, this function is useful if a very large object does not need to be fully stored. Conversely, unserialize () checks the existence of a function with the magic name _ wakeup. If yes, this function can reconstruct any resources that an object may have. _ Wakeup is used to reconstruct any database connections that may be lost during serialization and process other re-initialization tasks. 9. _ set_state when var_export () is called, this static method will be called (effective from PHP 5.1.0 ). The unique parameter of this method is an array, which contains values by array ('properties' => value ,...) Class attributes in the format. 10. When _ invoke tries to call an object by calling a function, the __invoke method is automatically called. PHP5.3.0 and later versions are effective. 11 and _ callStatic work in a way similar to _ call () magic method, __callstatic () is used to handle static method calls, PHP 5.3.0 and later versions effectively enhance the definition of the _ callStatic () method. it must be public and declared as static. Similarly, the __call () magic method must be defined as public, all other magic methods must be like this ========================================= ==========================================
PHP treats all class methods starting with _ (two underscores) as magic methods. Therefore, when you define your own class methods, do not use _ as the prefix.
?
1. _ construct ()
When an object is instantiated, the constructor of this object will be called first;
We know that the php5 object model and the function with the same class name are class construc functions. if the construc and _ construc () methods are defined at the same time, php5 calls _ contruct () by default instead of similar name functions. Therefore, _ contruct () is the default constructor of the class;
?
2. _ destruct ()
The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.
?
3. _ get (string $ name)
It is called when you try to read an attribute that does not exist. if you try to read an attribute that does not exist in an object, php will give an error message. If the _ get method is added to the class, and we can use this function to implement various operations similar to reflection in java.
?
4. _ set (string $ name, mixed $ value)
When an undefined variable is assigned a value, it is called.
?
5. _ call (string $ name, array $ arguments)
When an inaccessible method (such as undefined or invisible) is called, __call () is called.
_ CallStatic (string $ name, array $ arguments)
When an inaccessible method (such as undefined or invisible) is called in a static method, __callstatic () is called.
?
6. _ toString ()
This method is called when an object is printed. it is similar to the toString method of java. This function is called when the object is printed directly.
?
7. _ clone ()
Called when the object is cloned.
?
8. _ sleep ()
The serialize () function checks whether there is a magic method _ sleep. If yes, the __sleep () method will be called first before the serialization operation is executed. This function can be used to clear an object and return an array containing all the variable names of the object. If this method does not return any contentNULLSerialized, resulting in an E_NOTICE error. The _ sleep method is often used to submit uncommitted data or perform similar operations. At the same time, if you have some large objects that do not need to be saved, this function is very useful.
?
9. _ wakeup ()
In contrast to _ sleep (), unserialize () checks whether a _ wakeup method exists. If yes, the _ wakeup method is called to prepare the object data in advance. _ Wakeup is often used in deserialization operations, such as re-establishing a database connection or performing other initialization operations.
10. _ isset ()
When isset () or empty () is called for undefined variables, __isset () is called.
?
11. _ unset ()
Unset an object property is called. For example, unset ($ c-> name ).
?
12. _ set_state ()
Called when var_export is called. Use the return value of _ set_state as the return value of var_export.
?
13. _ autoload ()
When instantiating an object, if the corresponding class does not exist, this method is called. In short, it is the automatic loading of classes. when you try to use a class that is not organized by PHP, it will look for a global function of _ autoload. if this function exists, PHP uses a parameter to call it. the parameter is the name of the class.
?
14. _ invoke ()
When you try to call an object by calling a function, the __invoke method is automatically called.
?
Magic constant:
The current row number in the _ LINE _ File .?
The complete path and FILE name of The _ FILE. If it is used in a file to be included, the file name to be included is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path .?
The directory where the _ DIR _ file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0) =?
_ FUNCTION name (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, this value is always lowercase letters .?
_ CLASS name (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, this value is always lowercase letters .?
_ METHOD _ class METHOD name (new PHP 5.0.0 ). Returns the name (case sensitive) when this method is defined ).?
_ NAMESPACE _ name of the current NAMESPACE (case sensitive ). Is this constant defined during compilation (new in PHP 5.3.0 )?