Home > Article > Backend Development > Summary of common php magic method functions and usage
Overview
In Object-oriented programming, PHP provides a series of magic methods, which provide a lot of convenience for programming. Magic methods in PHP usually start with (two underscores) and do not require explicit calls but are triggered by some specific conditions. This article briefly summarizes the magic methods available in PHP.
Before starting
Before summarizing PHP’s magic methods, let’s define two classes for use in later examples:
The code is as follows :
<?php class Device { public $name; public $battery; public $data = array(); public $connection; protected function connect() { $this->connection = 'resource'; echo $this->name . ' connected' . PHP_EOL; } protected function disconnect() { $this->connection = null; echo $this->name . ' disconnected' . PHP_EOL; } } class Battery { private $charge = 0; public function setCharge($charge) { $charge = (int)$charge; if($charge < 0) { $charge = 0; } elseif($charge > 100) { $charge = 100; } $this->charge = $charge; } } ?>
The Device class has four member properties and two member methods. The Battery class has one member property and one member method.
Constructor and destructor
Constructor and destructor are called when the object is created and destroyed respectively. When an object is "destroyed", it means that there is no reference to the object. For example, if the variable that refers to the object is deleted (unset), reassigned, or the script execution ends, the destructor will be called.
construct()
construct()The constructor is by far the most commonly used function. When creating an object, you can do some initialization work in the constructor. You can define any number of parameters for the constructor, as long as the corresponding number of parameters is passed in when instantiating. Any exception that occurs in the constructor prevents the object from being created.
The code is as follows:
class Device { public function construct(Battery $battery, $name) { $this->battery = $battery; $this->name = $name; $this->connect(); } }
In the above example code, the constructor of the Device class assigns values to the member properties and calls the connect() method.
Declaring the constructor as a private method prevents objects from being created outside the class, which is often used in the simplex pattern.
desctruct()
The destructor is usually called when the object is destroyed. The destructor does not receive any parameters. Some cleanup work is often performed in the destructor, such as closing the database connection, etc.
Property Overloading
One thing to note is that "overloading" in PHP is not the same as overloading in most other languages. , although they all implement the same function.
The two magic methods involved in attribute overloading are mainly used to handle attribute access, defining what happens when we try to access a non-existent (or inaccessible) attribute.
get()
The magic method get() will be called when we try to access a property that does not exist. It receives a parameter that represents the name of the accessed attribute and returns the value of the attribute. In the Device class above, there is a data attribute, which plays a role here, such as the following code:
The code is as follows:
class Device { public function get($name) { if(array_key_exists($name, $this->data)) { return $this->data[$name]; } return null; } }
This magic method is the most A common place to do this is to extend access control by creating a "read-only" attribute. In the above Battery class, there is a private property $charge. We can extend this property through the get() magic method to be readable but not modifyable outside the class. The code is as follows:
The code is as follows:
class Battery { private $charge = 0; public function get($name) { if(isset($this->$name)) { return $this->$name; } return null; } }
set()
set() magic method when we try to modify an inaccessible It will be called when the attribute is called. It receives two parameters, one indicating the name of the attribute and one indicating the value of the attribute. The sample code is as follows:
The code is as follows:
class Device { public function set($name, $value) { // use the property name as the array key $this->data[$name] = $value; } }
isset()
isset() magic method is used to access an inaccessible property It is called when calling the isset() method, which receives a parameter representing the name of the attribute. It should return a Boolean value indicating whether the property exists. The code is as follows:
The code is as follows:
class Device { public function isset($name) { return array_key_exists($name, $this->data); } }
unset()
unset() magic method calls the unset() function to destroy a Called when a property is inaccessible, it receives a parameter representing the name of the property.
Convert object to string
Sometimes we need to express the object in the form of a string. If we print an object directly, the program will output an error message: PHP Catchable fatal error: Object of class Device could not be converted to string
toString()
toString() will be called when we use the object as a string. It does not receive any parameters. This method allows us to define the representation of the object. The code is as follows:
The code is as follows:
class Device { public function toString() { $connected = (isset($this->connection)) ? 'connected' : 'disconnected'; $count = count($this->data); return $this->name . ' is ' . $connected . ' with ' . $count . ' items in memory' . PHP_EOL; } ... }
set_state()(PHP 5.1)
Static magic method set_state(), in our This method is called when using the var_export() function to export an object. The var_export() function is used to convert PHP variables into PHP code. It receives an associative array containing object attribute values as a parameter. The sample code is as follows:
The code is as follows:
class Battery { //... public static function set_state(array $array) { $obj = new self(); $obj->setCharge($array['charge']); return $obj; } //... }
Clone object
默认的,对象都是按引用传值的。因此,在将一个对象赋值给另一个变量时,只是创建了指向该对象的一个引用,并没有复制该对象。为了实现真正得复制一个对象,我们需要使用clone关键字。
这种“按引用传递”的策略同样适用于包含在对象内部的对象。即使我们克隆了一个对象,在对象内部的任何对象都不会被克隆,因此最终的结果是两个对象共享了同一个内部对象。示例代码如下:
$device = new Device(new Battery(), 'iMagic'); $device2 = clone $device; $device->battery->setCharge(65); echo $device2->battery->charge; // 65
clone()
clone()魔术方法clone()可以解决上面的问题。当对一个对象使用clone关键字时,该魔术方法会被调用。在这个魔术方法里,我们可以实现任何子对象的克隆,代码如下:
代码如下:
class Device { ... public function clone() { // copy our Battery object $this->battery = clone $this->battery; } ... }
序列化是讲任意数据转换为字符串格式的过程。序列化通常用来将整个对象存入数据库或写入文件中。当反序列化存储的数据时,我们可以得到序列化之前的对象。但是,并不是所有得数据都可以被序列化,比如数据库连接。幸运的是,有一个魔术方法可以帮我们解决这个问题。
sleep()
魔术方法sleep()在对一个对象序列化时(调用serialize())会被调用。它不接收任何参数,而且应该返回一个包含所有应该被序列化的属性的数组。在该魔术方法中,也可以执行一些其他操作。
有一点要注意的是,不要再该函数中进行任何的析构操作,因为这可能会影响正在运行的对象。
示例代码如下:
class Device { public $name; public $battery; public $data = array(); public $connection; //... public function sleep() { return array('name', 'battery', 'data'); } //... }
wakeup()
魔术方法wakeup()在对存储的对象反序列化时会被调用。它不接收任何参数,也没有任何返回值。可以用它来处理在序列化时丢失的数据库连接或资源。代码如下:
代码如下:
class Device { //... public function wakeup() { // reconnect to the network $this->connect(); } //... }
方法重载
PHP还有两个与成员方法相关的魔术方法call()和callStatic(),这两个魔术方法类似于属性重载方法。
call()
魔术方法call()在调用不存在或不可访问的方法时会被调用。它接收两个参数,一个是调用的方法的名字,一个是包含函数参数的数组。我们可以使用这种方法调用子对象中得同名函数。
在这个例子中,要注意函数call_user_func_array(),这个函数允许我们动态调用一个命名的函数。
示例代码如下:
class Device { //... public function call($name, $arguments) { // make sure our child object has this method if(method_exists($this->connection, $name)) { // forward the call to our child object return call_user_func_array(array($this->connection, $name), $arguments); } return null; } //... }
callStatic()
魔术方法callStatic()与call()的功能一样,唯一不同的是,该方法在尝试访问一个不存在或不可访问的静态方法时会被调用。示例代码如下:
class Device { //... public static function callStatic($name, $arguments) { // make sure our class has this method if(method_exists('Connection', $name)) { // forward the static call to our class return call_user_func_array(array('Connection', $name), $arguments); } return null; } //... }
将对象作为函数
有时候我们会需要将对象作为函数使用。将对象作为函数使用,就像我们使用其他普通的函数一样,可以传参。
invoke()(PHP 5.3)
魔术方法invoke()在尝试将对象作为函数使用时会被调用。在该方法中定义的任何参数,都将被作为函数的参数。示例代码如下:
class Device { //... public function invoke($data) { echo $data; } //... } $device = new Device(new Battery(), 'iMagic'); $device('test'); // equiv to $device->invoke('test') // Outputs: test
其他:autoload()
autoload()方法并不是一个魔术方法,但是这个方法非常有用。但是,对着PHP版本的更新,该函数已经不建议使用,取而代之的是spl_auto_register()函数。
The above is the detailed content of Summary of common php magic method functions and usage. For more information, please follow other related articles on the PHP Chinese website!