PHP magic function execution time and sequence parsing

Source: Internet
Author: User
PHP magic function execution time and sequence analysis 1: Magic function 1. _ Construct () is called when an object is instantiated. When _ construct and a function with the same class name exist at the same time, __construct is called, and the other is not called. 2. _ Destruct () is called when an object or object operation is terminated. 3. _ Call () object calls a method. if a method exists, it is called directly. PHP magic function execution time and sequence parsing

I. magic functions

1. _ Construct ()
Called When instantiating an object,
When _ construct and a function with the class name both exist, __construct will be called and the other will not be called.
2. _ Destruct ()
It is called when an object or object operation is terminated.
3. _ Call ()
Object calls a method,
If a method exists, it is called directly;
If not, the _ call function is called.
4. _ Get ()
When reading the attributes of an object,
If a property exists, the property value is directly returned;
If not, the _ get function is called.
5. _ Set ()
When setting attributes of an object,
If an attribute exists, the value is assigned directly;
If not, the _ set function is called.
6. _ ToString ()
It is called to print or output an object. Such as echo $ obj; or print $ obj;
7. _ Clone ()
Called when cloning an object. For example, $ t = new Test (); $ t1 = clone $ t; 8. _ Sleep ()
Serialize is called before. If the object is large and you want to delete something and serialize it, consider this function.
9. _ Wakeup ()
Unserialize is called to initialize objects.
10. _ Isset ()
It is called to check whether an object property exists. For example, isset ($ c-> name ).
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.
2. magic constants
1. _ LINE __
Returns the current row number in the file.
2. _ FILE __
Returns the complete file path and file name. If it is used in a include file, the include file name is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path, while earlier versions sometimes contain a relative path.
3. _ FUNCTION __
Returns the 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.
4. _ CLASS __
The name of the returned class (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.
5. _ METHOD __
Method name of the returned class (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).

III. magic function instances

1. _ construct () when instantiating an object, this method of this object is called first.

class Test {  function __construct()  {   echo "before";  } } $t = new Test();

Output: start
We know that the php5 object model and the function with the same class name are class constructor. if we define the constructor and The _ construct () method at the same time, php5 calls the constructor by default instead of The _ construct () function. Therefore, _ construct () is the default constructor of the 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

We can release resources at the end of the object operation.

3. _ get () is called when an attempt is made 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 operations similar to reflection in java.

Class Test {public function _ get ($ key) {echo $ key. "does not exist" ;}}$ t = new Test (); echo $ t-> name; the output is: name does not exist

4. _ set () is called when you try to write a value to an attribute that does not exist.

Class Test {public function _ set ($ key, $ value) {echo ''. $ key. "Value ". $ value ;}}$ t = new Test (); $ t-> name = "aninggo"; the output is aninggo.

?

5. _ call () this method is called when an object does not exist.

Class Test {public function _ call ($ Key, $ Args) {echo "the {$ Key} method you want to call 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 want to call does not exist. The 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. We call this function when we print the object directly.

Class Test {public function _ toString () {return "print Test" ;}}$ t = new Test (); echo $ t;

When echo $ t; is run, $ t->__ toString () is called to output
Print Test

7. _ clone () is called when the object is cloned.

Class Test {public function _ clone () {echo "I have been copied! ";}}$ T = new Test (); $ t1 = clone $ t; program output: I have been cloned!

4. By the way, we will introduce several COOl experimental functions provided by php5.
(1) runkit_method_rename
This function dynamically changes the name of the called function.

Class Test {function foo () {return "foo! ";}} Runkit_method_rename ('test', // class name 'foo', // actually called function 'bar' // Display called function); echo Test :: bar (); the program will output 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 '$ num1, $ num2 ', // input the parameter 'return $ num1 + $ num2; ', // The executed 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 functions.

Class Foo {function example () {return "foo! ";}} Class Bar {// empty class} // Copy runkit_method_copy ('bar', 'Baz', 'foo', 'example '); // execute the Copy function echo Bar: baz ();

(4) runkit_method_redefine
Dynamically modify the return value of a function
This function allows us to easily implement MOCK testing for classes! Is it COOL?

Class Example {function foo () {return "foo! ";}}// Create a test object $ e = new Example (); // output echo" Before: "Before the test object :". $ e-> foo (); // modify the return value runkit_method_redefine ('example ', 'foo', '', 'Return" bar! "; ', RUNKIT_ACC_PUBLIC); // execute echo" After: ". $ e-> foo ();

(5) runkit_method_remove
This function is very simple. you can see it by name and dynamically remove the function from the class.

Class Test {function foo () {return "foo! ";} Function bar () {return" bar! ";}} // Remove the foo function runkit_method_remove ('test', 'Foo'); echo implode ('', get_class_methods ('test'); bar of program output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.