Summary of common magic methods in php and php magic Summary
This article summarizes common magic methods in php in detail and shares them with you for your reference. The specific content is as follows:
1. PHP regards all methods starting with "_" as magic methods, so any custom methods cannot start "_".
Php overload refers to the dynamic creation of attributes or methods. Overloading is implemented by magic. Parameters of these magic methods cannot be passed, __get () ,__ set () ,__ isset () ,__ unset (), to implement class attribute overloading.
2. The _ get () method is called when an inaccessible attribute in the response class
3. When an inaccessible attribute in the category class, _ get () ,__ set () ,__ isset () ,__ unset () will be called according to different situations (), when the isset () or empty () function is used to access the attribute, when the _ isset () method is called to use the unset () function, _ unset () is called () method.
4. The four methods cannot be defined using the static keyword. After php5.3.0, a warning at the warning level will occur. When the static method is used to define the four methods.
5. What is an inaccessible attribute? (1) attributes not in the class; (2) private variables are accessed outside the class. Method Overloading is implemented through _ call () and _ callstatic (). The difference between the two is whether the Context Environment is a static method, the latter is called when an inaccessible static method is called. _ Callstatic () is added in php5.3.0.
6. _ sleep () and _ wakeup ()
During serialize () serialization, the system first checks whether the _ sleep () function exists. If yes, the system first calls _ sleep () to return an array containing all variable names, if this method does not return any content, null is serialized and a notice-level error is generated.
The _ wakeup () method is opposite to _ sleep (). It is called when unserialize () is called.
7. _ toString (): let a class decide how to convert it to a string. Before php5.2.0, this method can only take effect in combination with the echo or print method. After php5.2.0, it can combine printf () but cannot use non-character modifiers such as % d.
8. _ invoke (). When you try to call an object by calling a function, this function is started and takes effect only after php5.3.
9. _ set_state () static method. This static method is called only when var_export () is called. It will take effect after php5.1.0. The unique parameter is to receive an array.
10. _ construct () and _ destruct (), constructor and destructor are mainly used for class initialization, the Destructor runs when all references to an object are deleted or the object is destroyed. An exception cannot be thrown in the Destructor; otherwise, a fatal error is triggered. The Destructor is called at the end of the script. At this time, all http headers have been sent.
11. _ clone (): Object replication. This method cannot be called directly. It must be displayed and called using the clone keyword.
Code:
// When instantiating an Object, the _ construct Method $ obj = new Object () of the Object will be called; // when assigning values to attributes that do not exist in the Object, the _ set Method of the object $ obj-> title = "hello"; // when you use an attribute that does not exist, the _ get method of the object echo $ obj-> title; // when the method of the object does not exist, the _ call method $ obj-> test ("hello", 123,567) of the object is called. // when a non-existing static method of the class is called, when the _ callStatic method Object: staticMethod ("static", "not found method") of the class is called; // when the Object is output directly, the _ toString method echo $ obj of the object will be called; // when an object is used as a function, the _ invoke method echo $ obj ("test") of the object will be called "); // when cloning an object, the _ clone method of the object is called $ obj2 = clone $ obj; // when the object property exists, the _ isset method var_dump (isset ($ obj-> aaa) of the object will be called; // when the object attribute is destroyed, the _ unset method unset ($ obj-> aaa) of the Object is called. class Object {protected $ array = array (); function _ construct () {echo _ METHOD __. "You are instantiating an object <br>";} function _ set ($ name, $ value) {$ this-> array [$ name] = $ value ;} function _ get ($ name) {return $ this-> array [$ name];} function _ call ($ name, $ arguments) {var_dump ($ name, $ arguments); return "magic function \ n";} static function _ callStatic ($ name, $ arguments) {var_dump ($ name, $ arguments ); return "magic static function";} function _ toString () {return _ CLASS __. '<br>';} function _ invoke ($ arguments) {var_dump ($ arguments); return _ METHOD __. '<br>';} function _ clone () {echo _ METHOD __. "You are cloning the object <br>";} function _ isset ($ name) {echo _ METHOD __. "You want to determine whether there are any attributes ". $ name. "<br>"; return 1;} function _ unset ($ name) {echo _ METHOD __. "You want to delete attributes ". $ name. "<br>";} function _ destruct () {echo _ METHOD __. "You are canceling the object <br> ";}}
The above is all about the common magic methods in php, and I hope it will help you learn.
Articles you may be interested in:
- Php magic methods
- Describes how to use php magic methods _ get () and _ set ()
- PHP5 magic constants and magic methods
- In-depth explanation of magic methods in PHP5
- Summary of common magic methods in php
- Magic Method in PHP
- PHP magic method _ GET, _ SET use instance
- PHP magic methods _ ISSET and _ UNSET use instances
- Common php magic methods