The Magic functions in PHP include the ToString, call, clone, autoload several commonly used functions, I hope this tutorial will be helpful to you friends.
Summary of object-oriented development
1. Description and configuration of the object. (Can be understood as the output text description of the object)
2. A processing of object methods. (It can be understood that when an exception occurs, you customize a more user-friendly error message)
3. The application of the cloned object. (It can be understood that the same object is cloned on the original object, and note that the other is another.) )
4. Methods for automatically loading objects. (Can be understood as a reference)
1. Object description and Configuration
Method Name: __tostring ()
Format:
| The code is as follows |
Copy Code |
Class My { function __tostring () { Return "Write a literal description of this class here"; It's going to go wrong with Return,echo. } } $p = new My (); Echo $p; |
Instance:
| The code is as follows |
Copy Code |
Class My { function __tostring () { Return "The role of this class is to defend the earth. "; } } $p = new My (); Echo $p; ?> |
2. Exception handling for object methods
Call some non-existent object methods exception handling, is the program normal operation.
Method Name: __call ($funname, $arr _value)
Format:
| The code is as follows |
Copy Code |
Class My { function __call ($n, $v) { echo "Wrong method Name:". $n; echo "Wrong parameter:". $v; } } |
Instance:
| The code is as follows |
Copy Code |
Class My { function __tostring () { Return "The role of this class is to defend the earth. "; } function __call ($n, $v) { echo "Wrong way ". $n. " "; echo "Wrong value ". Print_r ($v). ""; The value is passed as an array, so use Print_r. } } $p = new My (); $p->demo ("First", 6); ?> |
3. Cloning of objects
Cloning allows you to generate two identical objects in memory or upgrade the original objects. (Not simple assignment, but two memory in memory, clones and clones are 2 different attribute methods of the same two objects)
Method Name: __clone ()
Keyword: clone
Format:
| The code is as follows |
Copy Code |
Class My { function __clone () { echo "function called automatically when cloning"; } } $a = new My (); $b = Clone $a; |
Instance:
| The code is as follows |
Copy Code |
Class My { Public $name = "Piglet"; function __tostring () { Return "The role of this class is to defend the earth. "; } function __call ($n, $v) { echo "Wrong way ". $n. " "; echo "Wrong value ". Print_r ($v). ""; The value is passed as an array, so use Print_r. } } $p = new My (); $b = Clone $p; echo $b->name= "Piggy Head". "; This is the cloned and modified object, if used $b = $p, then modify $b->name= "Piglet Head", and then output $p, will also be modified. Echo $p->name; This is the original object, it has been cloned by $b, the others are the same, but they are independent. ?> |
4. Methods for automatically loading objects
Quickly get the object name and automatically load into the current page
Method Name: __autoload ()
Format:
| The code is as follows |
Copy Code |
function __autoload ($class _n) { Include ($class _n. PHP "); } $p = new MyPc (); Automatic calls are performed as follows $d = new Demo (); Include ("mypc.php") |
Instance:
| The code is as follows |
Copy Code |
function __aotoload ($class _name) { Include ($class _name. PHP "); Written outside of the class. } $p = new Class_name (); The class name instantiated here is replaced by the above function variable $class _name, which is very convenient. ?> |
http://www.bkjia.com/PHPjc/632640.html www.bkjia.com true http://www.bkjia.com/PHPjc/632640.html techarticle The magic functions in PHP include the ToString, call, clone, autoload several commonly used functions, I hope this tutorial will be helpful to you friends. Object-Oriented Development summary 1. Description and configuration of objects ...