Home > Backend Development > PHP Tutorial > Summary of common php magic method functions and usage

Summary of common php magic method functions and usage

伊谢尔伦
Release: 2023-03-12 13:56:01
Original
1462 people have browsed it

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 = &#39;resource&#39;;
        echo $this->name . &#39; connected&#39; . PHP_EOL;
    }
 
    protected function disconnect() {
        $this->connection = null;
        echo $this->name . &#39; disconnected&#39; . 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;
    }
}
?>
Copy after login


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();
    }
}
Copy after login

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;
    }
}
Copy after login

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;
    }
}
Copy after login

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;
    }
}
Copy after login

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);
    }
}
Copy after login

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)) ? &#39;connected&#39; : &#39;disconnected&#39;;
       $count = count($this->data);
       return $this->name . &#39; is &#39; . $connected . &#39; with &#39; . $count . &#39; items in memory&#39; . PHP_EOL;
    }
    ...
}
Copy after login

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[&#39;charge&#39;]);
        return $obj;
    }
    //...
}
Copy after login

Clone object

默认的,对象都是按引用传值的。因此,在将一个对象赋值给另一个变量时,只是创建了指向该对象的一个引用,并没有复制该对象。为了实现真正得复制一个对象,我们需要使用clone关键字。
这种“按引用传递”的策略同样适用于包含在对象内部的对象。即使我们克隆了一个对象,在对象内部的任何对象都不会被克隆,因此最终的结果是两个对象共享了同一个内部对象。示例代码如下:

$device = new Device(new Battery(), &#39;iMagic&#39;);
$device2 = clone $device;
 
$device->battery->setCharge(65);
echo $device2->battery->charge;
// 65
Copy after login

clone()

clone()魔术方法clone()可以解决上面的问题。当对一个对象使用clone关键字时,该魔术方法会被调用。在这个魔术方法里,我们可以实现任何子对象的克隆,代码如下:

代码如下:

class Device {
    ...
    public function  clone() {
        // copy our Battery object
        $this->battery = clone $this->battery;
    }
    ...
}
Copy after login

对象序列化

序列化是讲任意数据转换为字符串格式的过程。序列化通常用来将整个对象存入数据库或写入文件中。当反序列化存储的数据时,我们可以得到序列化之前的对象。但是,并不是所有得数据都可以被序列化,比如数据库连接。幸运的是,有一个魔术方法可以帮我们解决这个问题。

sleep()

魔术方法sleep()在对一个对象序列化时(调用serialize())会被调用。它不接收任何参数,而且应该返回一个包含所有应该被序列化的属性的数组。在该魔术方法中,也可以执行一些其他操作。
有一点要注意的是,不要再该函数中进行任何的析构操作,因为这可能会影响正在运行的对象。

示例代码如下:

class Device {
    public $name;           
    public $battery;       
    public $data = array();
    public $connection;    
    //...
    public function  sleep() {
        return array(&#39;name&#39;, &#39;battery&#39;, &#39;data&#39;);
    }
    //...
}
Copy after login

wakeup()

魔术方法wakeup()在对存储的对象反序列化时会被调用。它不接收任何参数,也没有任何返回值。可以用它来处理在序列化时丢失的数据库连接或资源。代码如下:

代码如下:

class Device {
    //...
    public function  wakeup() {
        // reconnect to the network
        $this->connect();
    }
    //...
}
Copy after login

方法重载

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;
    }
    //...
}
Copy after login

callStatic()

魔术方法callStatic()与call()的功能一样,唯一不同的是,该方法在尝试访问一个不存在或不可访问的静态方法时会被调用。示例代码如下:

class Device {
    //...
    public static function  callStatic($name, $arguments) {
        // make sure our class has this method
        if(method_exists(&#39;Connection&#39;, $name)) {
            // forward the static call to our class
            return call_user_func_array(array(&#39;Connection&#39;, $name), $arguments);
        }
        return null;
    }
    //...
}
Copy after login

将对象作为函数

有时候我们会需要将对象作为函数使用。将对象作为函数使用,就像我们使用其他普通的函数一样,可以传参。

invoke()(PHP 5.3)

魔术方法invoke()在尝试将对象作为函数使用时会被调用。在该方法中定义的任何参数,都将被作为函数的参数。示例代码如下:

class Device {
    //...
    public function invoke($data) {
        echo $data;
    }
    //...
}
$device = new Device(new Battery(), &#39;iMagic&#39;);
$device(&#39;test&#39;);
// equiv to $device->invoke(&#39;test&#39;)
// Outputs: test
Copy after login

其他: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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template