PHP learning (2): PHP magic method 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;
Header ('content-type: text/html; charset = utf-8 '); class person {private $ name; /*** constructor-called when you call new person to create an object. * @ param string */function _ construct ($ name = 'a') {echo 'Call constructor '. $ name ;}$ obj = new person (); // The "call constructor" is output.
2. _ destruct ()
The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.
Header ('content-type: text/html; charset = utf-8 '); class person {private $ _ name; function _ construct ($ val) {$ this-> _ name = $ val;} function _ destruct () {echo $ this-> _ name. 'Call the destructor
';}}$ Obj1 = new person ('1'); $ obj2 = new person ('2'); $ obj3 = new person ('3 '); // because it is stored in the memory as a heap, the principle of structure analysis is post-first-out, so obj3 is first destructed, output the following // 3 call the destructor // 2 call the destructor // 1 call the destructor
Header ('content-type: text/html; charset = utf-8 '); class person {private $ _ name; function _ construct ($ val) {$ this-> _ name = $ val;} function _ destruct () {echo $ this-> _ name. 'Call the destructor
';}}$ Obj1 = new person ('1'); $ obj2 = new person ('2'); unset ($ obj2 ); $ obj3 = new person ('3'); // it is worth noting that, if you use unset to remove obj2 in the middle or use obj2 = null to display and remove the following output: // The Destructor obj2 is executed first and then the obj3/2 is executed to call the destructor. // 3 call the destructor // 1 call the destructor
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.
Header ('content-type: text/html; charset = utf-8 '); class person {private $ name; function _ get ($ val) {return 'No attribute for this name '. $ val ;}$ obj = new person (); echo $ obj-> name; // when you call an attribute that does not exist to output the value of this attribute, you can call the _ get () magic function $ val, which is the name of the property you call. // it is worth noting that no private modifier exists or is defined, the same is true if the subclass inherits the parent class. // output: The property name without this name is not obtained.
4. _ set (string $ name, mixed $ value)
When an undefined variable is assigned a value, it is called.
Header ('content-type: text/html; charset = utf-8 '); class person {private $ name;/*** assign a value to an undefined attribute parameter 1 = attribute name, parameter 2 = value (any type) * @ param string $ key * @ param mixed $ val */function _ set ($ key, $ val) {return 'attributes not defined '. $ key. 'assignment '. $ val ;}$ obj = new person (); echo $ obj-> age = 'Big pig '; // output: Big Pig
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 content, NULL is serialized, 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, the 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, the value is always lowercase letters.
_ METHOD _ class METHOD name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).
_ NAMESPACE _ name of the current NAMESPACE (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0)