In php, magic functions include tostring, call, clone, and autoload. I hope this tutorial will be helpful to all of you.
Object-Oriented development Summary
1. Description and configuration of the object. (It can be understood as the output text description of the object)
2. processing an object method. (It can be understood that, when an exception occurs, a more user-friendly error message is customized)
3. Application of the cloned object. (It can be understood that, on the original object, the same object is cloned, And the other object is the same .)
4. Methods for automatically loading objects. (Can be understood as reference)
1. Object Description and Configuration
Method Name __tostring ()
Format:
| The Code is as follows: |
Copy code |
Class My { Function _ tostring (){ Return "write the text description of this class here"; // if you want to use return, echo will fail. } } $ P = new My (); Echo $ p; |
Instance:
| The Code is as follows: |
Copy code |
<? Php Class My { Function _ toString (){ Return "is used to protect the Earth. "; } } $ P = new My (); Echo $ p; ?> |
2. Exception Handling of object Methods
Exception Handling for calling some non-existing object methods is that the program runs normally.
Method Name: __ call ($ funname, $ arr_value)
Format:
| The Code is as follows: |
Copy code |
Class My { Function _ call ($ n, $ v ){ Echo "incorrect method name:". $ n; Echo "Incorrect Parameter:". $ v; } } |
Instance:
| The Code is as follows: |
Copy code |
<? Php Class My { Function _ toString (){ Return "is used to protect the Earth. "; } Function _ call ($ n, $ v ){ Echo "incorrect method <B>". $ n. "</B> <br/> "; Echo "incorrect value <B>". print_r ($ v). "</B>"; // The value is transmitted in array mode, so print_r is used. } } $ P = new My (); $ P-> demo ("first", 6 ); ?> |
3. Object cloning
By cloning, you can generate two identical objects in the memory or upgrade the original object. (Not a simple assignment, but two memory blocks are opened in the memory. The cloned and cloned two objects are the same as the two Property methods)
Method Name: __ clone ()
Keyword: clone
Format:
| The Code is as follows: |
Copy code |
Class My { Function _ clone (){ Echo "functions automatically called during cloning "; } } $ A = new My (); $ B = clone $; |
Instance:
| The Code is as follows: |
Copy code |
<? Php Class My { Public $ name = ""; Function _ toString (){ Return "is used to protect the Earth. "; } Function _ call ($ n, $ v ){ Echo "incorrect method <B>". $ n. "</B> <br/> "; Echo "incorrect value <B>". print_r ($ v). "</B>"; // The value is transmitted in array mode, so print_r is used. } } $ P = new My (); $ B = clone $ p; Echo $ B-> name = "pig ". "<br/>"; // This is the cloned and modified object. If $ B = $ p is used, modify $ B-> name = "Pigsty head, the $ p output will also be modified. Echo $ p-> name; // This is the original object. It has been cloned by $ B. Others are the same, but they are independent. ?> |
4. Methods for automatically loading objects
Get the object name quickly and load it into the current page automatically
Method Name __autoload ()
Format:
| The Code is as follows: |
Copy code |
Function _ autoload ($ class_n ){ Include ($ class_n. ". php "); } $ P = new MyPc (); // The automatic call is executed as follows $ D = new demo (); // include ("mypc. php ") |
Instance:
| The Code is as follows: |
Copy code |
<? Php Function _ aotoload ($ class_name ){ Include ($ class_name. ". php"); // write it outside the class. } $ P = new class_name (); // The class name instantiated here is replaced by the above function variable $ class_name, which is very convenient. ?> |