Home >Backend Development >PHP Tutorial >Introduction to PHP's magic functions and magic constants_PHP Tutorial
Called when instantiating an object. When __construct and a function with the class name and function name exist at the same time, __construct will be called and the other will not be called.
Called when an object is deleted or the object operation terminates.
The object calls a method, If the method exists, call it directly; If it does not exist, the __call function will be called.
When reading the properties of an object, If the attribute exists, the attribute value is returned directly; If it does not exist, the __get function will be called.
When setting the properties of an object, If the attribute exists, assign it directly; If it does not exist, the __set function will be called.
Called when printing an object. Such as echo $obj; or print $obj;
Called when cloning an object. For example: $t=new Test();$t1=clone $t;
serialize was called before. If the object is relatively large and you want to delete a little bit before serializing it, you can consider this function.
Called when unserialize is used to do some object initialization work.
Called when checking whether a property of an object exists. Such as: isset($c->name).
Called when unsetting a property of an object. For example: unset($c->name).
Called when var_export is called. Use the return value of __set_state as the return value of var_export.
When instantiating an object, if the corresponding class does not exist, this method is called.
Returns the current line number in the file.
Returns the full path and file name of the file. If used in an include file, returns the include file name. As of PHP 4.0.2, __FILE__ always contains an absolute path, while versions before that sometimes contained a relative path.
Return function name (new in PHP 4.3.0). Since PHP 5 this constant returns the name of the function as it was defined (case sensitive). In PHP 4 this value is always lowercase.
Returns the name of the class (new in PHP 4.3.0). Since PHP 5 this constant returns the name of the class when it was defined (case sensitive). In PHP 4 this value is always lowercase.
Returns the method name of the class (newly added in PHP 5.0.0). Returns the name of the method as it was defined (case-sensitive).
First introduction to magic methods
Php5.0 has provided us with many object-oriented features since its release, especially many easy-to-use magic methods that allow us to simplify our coding and better design our systems. Today we will learn about the magic methods provided by php5.0.
__get() is called when trying to read a property that does not exist.
If you try to read a property that does not exist in an object, PHP will give an error message. If we add the __get method to the class, we can use this function to implement various operations similar to reflection in Java.
class Test { public function __get($key) { echo $key . " 不存在"; } } $t = new Test(); echo $t->name;
will output: name does not exist
__set() is called when trying to write a value to a property that does not exist.
class Test { public function __set($key,$value) { echo '对'.$key . "附值".$value; } } $t = new Test(); $t->name = "aninggo";
will output: Attach value to name aninggo
__call() This method is called when trying to call a method that does not exist on the object.
class Test { public function __call($Key, $Args) { echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true); } } $t = new Test(); $t->getName(aning,go);
The program will output: The getName method you are trying to call does not exist.
The parameters are:
Array ( [0] => aning [1] => go )
__toString() is called when printing an object
This method is similar to java's toString method. This function is called back when we print the object directly
class Test { public function __toString() { return "打印 Test"; } } $t = new Test(); echo $t;
When running echo $t;, $t->__toString(); will be called to output : Print Test
__clone() is called when the object is cloned
class Test { public function __clone() { echo "我被复制了!"; } } $t = new Test(); $t1 = clone $t;
Program output: I was copied