PHP Magic function Execution time and sequence parsing
One: Magic function
1. __construct ()
Called when an object is instantiated,
When __construct and a function with a class name are present, __construct is called and the other is not called.
2. __destruct ()
Called when an object is deleted or when an object operation terminates.
3. __call ()
Object calls a method,
If the method exists, it is called directly;
If it does not exist, it will call the __call function.
4. __get ()
When reading the properties of an object,
If the attribute exists, the property value is returned directly;
If it does not exist, the __get function is called.
5. __set ()
When you set the properties of an object,
If the attribute exists, the value is directly assigned;
If it does not exist, the __set function is called.
6. __tostring ()
Called when printing or outputting an object. such as Echo $obj; or print $obj;
7. __clone ()
Called when the object is cloned. such as: $t =new Test (); $t 1=clone $t; 8. __sleep ()
Serialize before being called. If the object is relatively large, want to cut a bit of the east and then serialize, you can consider this function.
9. __wakeup ()
Unserialize is called to do some initialization of the object.
10. __isset ()
Called when detecting whether an object's properties exist. such as: Isset ($c->name).
11. __unset ()
Called when a property of an object is unset. such as: unset ($c->name).
12. __set_state ()
Called when the Var_export is called. Use the return value of __set_state as the return value of Var_export.
13. __autoload ()
When an object is instantiated, the method is called if the corresponding class does not exist.
Two: Magic constant
1. __line__
Returns the current line number in the file.
2. __file__
Returns the full path and file name of the file. If used in the include file, the include filename is returned. Since PHP 4.0.2, __file__ always contains an absolute path, and the previous version sometimes contains a relative path.
3. __function__
Returns the name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.
4. __class__
Returns the name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, this value is always in lowercase letters.
5. __method__
Returns the method name of the class (PHP 5.0.0 new). Returns the name of the method when it is defined (case-sensitive).
Three: Magic Function example
1.__construct () When an object is instantiated, this method of the object is called first.
Class Test { function __construct () { echo "before"; }} $t = new Test (); |
The output is: start
We know that the PHP5 object model and the same class name function are the constructors of the class, so if we define both the constructor and the __construct () method, PHP5 will call the constructor by default instead of calling the __construct () function, so __construct () As the default constructor for a class
2.__destruct () This method is called when an object or object operation is terminated.
Class Test { function __destruct () { echo "end"; }} $t = new Test (); the end will be output |
We can do things like freeing resources at the end of an object operation.
3.__get () is called when attempting to read a property that does not exist.
If you try to read a property that does not exist for an object, PHP will give you an error message. If you add a __get method to a class, and we can use this function to implement various actions like reflection in Java.
Class Test {public function __get ($key) { echo $key. "does not exist"; } } $t = new Test (); Echo $t->name; It will output: name does not exist |
4.__set () is called when an attempt is made to write a value to a property that does not exist.
Class Test {public function __set ($key, $value) { echo ' pair '. $key. "Attached value". $value; } } $t = new Test (); $t->name = "Aninggo"; Will output: The value of the name attached to the Aninggo |
?
5.__call () Call this method when attempting to invoke a method that does not exist for an object.
Class Test {public function __call ($Key, $Args) { echo "the {$Key} method you are calling does not exist. The parameter you passed in is: ". Print_r ($Args, True); } } $t = new Test (); $t->getname (Aning,go); |
The program will output:
The GetName method you are calling does not exist. parameter is: Array
(
[0] = aning
[1] = = Go
)
6.__tostring () is called when an object is printed
This method is similar to the ToString method of Java, when we directly print the object callback with this function
Class Test {public function __tostring () { return "print Test"; |
When the Echo $t is run, $t->__tostring () is called and the output
Print Test
7.__clone () is called when the object is cloned
Class Test {public function __clone () { echo "I was copied! "; } } $t = new Test (); $t 1 = clone $t; program output: I was cloned!
Four By the way, some of the very cool experimental functions provided in PHP5 are
(1) Runkit_method_rename
This function can dynamically change the name of the function we call.
Class Test { function foo () { return "Foo!" }} runkit_method_rename ( ' Test ',//class name ' foo '),// The actual called function ' bar '//shows the function called); echo Test::bar (); program outputs Foo! |
(2) Runkit_method_add
This function can dynamically add functions to the class.
Class Test { function foo () { return "Foo!" }} runkit_method_add ( Test,// class name ' Add ',//new function name) ' $num 1, $num 2 ',//pass in the parameter ' return $num 1 + $num 2; ',//execute code runkit_acc_public);//Call Echo $e->add (12, 4); |
(3) Runkit_method_copy
You can copy the functions in Class A to Class B and rename the function.
Class Foo { Function example () { return "foo!"; }} Class Bar { ///Empty class}//performs copy Runkit_method_copy (' Bar ', ' baz ', ' Foo ', ' example ');//Perform copy function echo Bar::baz (); |
(4) Runkit_method_redefine
Dynamically changing the return value of a function
This function allows us to easily implement mock tests of classes! Isn't it cool?
Class Example { function foo () { return "foo!"; }} Create a Test object $e = new Example ();//Output echo "before:" Before testing the object. $e->foo ();//Modify the return value Runkit_method_redefine ( ' Example ', ' foo ', ', ' return ' bar! '; ', runkit_ Acc_public);//Perform output echo "after:". $e->foo (); |
(5) Runkit_method_remove
This function is very simple, see the name can be seen, the dynamic removal of the function from the class
Class Test {function foo () {return "Foo!"} function bar () {return "bar!";}}//remove Foo function runkit_method_remove (' Tes T ', ' foo '); echo implode (', Get_class_methods (' Test ')); Program Output Bar