Magic function
- __construct ()
Called when an object is instantiated, and when __construct and a function with the name of the class name are present, __construct is called and the other is not called.
- __destruct ()
Called when an object is deleted or when an object operation terminates.
- __call ()
The object calls a method, and if the method exists, it is called directly; if it does not exist, it will call the __call function.
- __get ()
When a property of an object is read, the property value is returned directly if the property exists, or the __get function is called if it does not exist.
- __set ()
When a property of an object is set, the value is assigned directly if the property exists, or the __set function is called if it does not exist.
- __tostring ()
Called when an object is printed. such as Echo $obj; or print $obj;
- __clone ()
Called when the object is cloned. such as: $t =new Test (); $t 1=clone $t;
- __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.
- __wakeup ()
Unserialize is called to do some initialization of the object.
- __isset ()
Called when detecting whether an object's properties exist. such as: Isset ($c->name).
- __unset ()
Called when a property of an object is unset. such as: unset ($c->name).
- __set_state ()
Called when the Var_export is called. Use the return value of __set_state as the return value of Var_export.
- __autoload ()
When an object is instantiated, the method is called if the corresponding class does not exist.
Magic Constants
- __line__
Returns the current line number in the file.
- __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.
- __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.
- __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.
- __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).
The method of first knowledge magic
Php5.0 has provided us with a lot of object-oriented features since its release, especially for our easy-to-use magic methods that allow us to simplify our coding and better design our systems. Today we will come to know the Magic method that php5.0 offers us.
__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
__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
__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 want to call does not exist.
The parameters are:
Array ([0] = aning[1] = = Go)
__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";}} $t = new Test (); Echo $t;
When the Echo $t is run, $t->__tostring () is called and output: print Test
__clone () is called when the object is cloned
Class Test{public function __clone () { echo "I've been copied!" ";}} $t = new Test (), $t 1 = clone $t;
Program output: I've been copied.
http://www.bkjia.com/PHPjc/752480.html www.bkjia.com true http://www.bkjia.com/PHPjc/752480.html techarticle The Magic function __construct () is called when an object is instantiated, and when __construct and a function with a class name are present, __construct is called and the other is not called. __destr ...