PHP provides a series of magic methods for object-oriented programming, in PHP, the methods starting with two underscores _ are called Magic methods. these magic methods do not need to be called, but start with a specific condition. This chapter gives you a brief introduction to the magic methods provided in PHP.
What is magic?
PHP provides a series of magic methods for object-oriented programming, in PHP, the methods starting with two underscores _ are called Magic methods. these magic methods do not need to be called, but start with a specific condition. This chapter gives you a brief introduction to the magic methods provided in PHP.
The following table lists the PHP magic methods:
Function |
Description |
_ Construct () |
Class constructor |
_ Destruct () |
Class destructor |
_ Call () |
Called when an inaccessible method is called in an object |
_ CallStatic () |
Call an inaccessible method in static mode |
_ Get () |
Called when obtaining a class member variable |
_ Set () |
Called when setting a class member variable |
_ Isset () |
When isset () or empty () is called for an inaccessible attribute |
_ Unset () |
This API is called when unset () is called for an inaccessible attribute. |
_ Sleep () |
This function is called first when serialize () is executed. |
_ Wakeup () |
This function is called first when unserialize () is executed. |
_ ToString () |
Response method when the class is treated as a string |
_ Invoke () |
Method of calling a function to call an object |
_ Set_state () |
This static method is called when var_export () is called to export the class. |
_ Clone () |
Called when the object copy is complete |
In the table above, "_" is two underscores instead of one "_".
Next, let's take a look at the examples and applications of these magic methods.
_ Construct () and _ destruct ()
Constructor and destructor should not be unfamiliar. they are called when the object is created or eliminated. For example, we need to open a file, which is opened when the object is created, and closed when the object is killed.
handle = fopen(...); } function __destruct(){ fclose($this->handle); }}?>
These two methods can be extended during inheritance, for example:
_ Call () and _ callStatic ()
When an inaccessible method is called in an object, the two methods are called, and the latter is a static method. These two methods may be used in Variable functions calls.
runTest('in object context');MethodTest::runTest('in static context');?>
_ Get () ,__ set () ,__ isset () and _ unset ()
These two functions are called when get/set is a member variable of a class. For example, we save the object variables in another array, instead of the member variables of the object.
data[$name] = $value; } public function __get($name){ if(array_key_exists($name, $this->data)) return $this->data[$name]; return NULL; } public function __isset($name){ return isset($this->data[$name]); } public function __unset($name){ unset($this->data[$name]); }}?>
_ Sleep () and _ wakeup ()
When we execute serialize () and unserialize (), these two functions are called first. For example, when we serialize an object, this object has a database link. to restore the link status in deserialization, we can reconstruct these two functions to restore the link.
Example:
server = $server; $this->username = $username; $this->password = $password; $this->db = $db; $this->connect(); } private function connect() { $this->link = mysql_connect($this->server, $this->username, $this->password); mysql_select_db($this->db, $this->link); } public function __sleep() { return array('server', 'username', 'password', 'db'); } public function __wakeup() { $this->connect(); }}?>
_ ToString ()
Response method when the object is treated as a string. For example, echo $ obj; is used to output an object.
This method can only return a string and cannot throw an exception in this method. Otherwise, a fatal error occurs.
_ Invoke ()
The response method when a function is called. As follows:
_ Set_state ()
This static method is called when var_export () is called to export the class.
var1 = $an_array['var1']; $obj->var2 = $an_array['var2']; return $obj; }}$a = new A;$a->var1 = 5;$a->var2 = 'foo';var_dump(var_export($a));?>
_ Clone ()
Called when the object copy is complete. For example, detailed description of the design mode and PHP implementation: This function is used to prevent the object from being cloned.
Recommended articles:
Examples of magic constants in php
The above is a detailed explanation of the magic method examples in php. For more information, see other related articles in the first PHP community!