Briefly describe the magic methods provided in PHP

墨辰丷
Release: 2023-03-31 07:32:01
Original
1370 people have browsed it

This article mainly introduces the magic methods provided in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

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 certain conditions.

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 can prevent 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() is 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

The most commonly used place for this magic method Access control is extended by creating a "read-only" attribute. In the above Battery class, there is a private property $charge, which we can extend 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 will happen when we try to modify an inaccessible property When called, it receives two parameters, one representing the name of the attribute and one representing 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() The magic method calls isset() on an inaccessible property. ) method will be called, it receives a parameter indicating 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() The magic method calls the unset() function to destroy an inaccessible object. It is called when a property is called, and 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(), when we use var_export () function will call this method when outputting the 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()函数。

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php针对数组的删除、转换、分组、排序

php针对文件操作及字符串加密的方法

php模拟post请求的三种常见用法

The above is the detailed content of Briefly describe the magic methods provided in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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