Home >Backend Development >PHP Problem >What are the magic methods of php?

What are the magic methods of php?

(*-*)浩
(*-*)浩Original
2019-10-09 10:53:462852browse

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.

What are the magic methods of php?

##__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), Methods such as __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() are called Magic methods in PHP. (Recommended learning: PHP video tutorial)

Note: You cannot use these method names when naming your own class methods, unless you want to use its magic Function.

Note: PHP reserves all class methods starting with __ (two underscores) as magic methods. Therefore, when defining class methods, except for the above magic methods, it is recommended not to prefix them with __.

__sleep() and __wakeup()

public __sleep ( void ) : array
__wakeup ( void ) : void

serialize() function checks whether there is a magic method __sleep() in the class. If present, this method will be called first, and then the serialization operation will be performed. This function can be used to clean an object and return an array containing the names of all variables in the object that should be serialized. If the method returns nothing, NULL is serialized and an E_NOTICE level error is raised.

Note:

__sleep() cannot return the name of the private member of the parent class. Doing so will generate an E_NOTICE level error. The Serializable interface can be used instead.

__sleep() method is often used to submit uncommitted data, or similar cleanup operations. At the same time, this function is useful if you have some large objects but do not need to save them all.

In contrast, unserialize() checks whether there is a __wakeup() method. If it exists, the __wakeup method will be called first to prepare the resources needed by the object in advance.

__wakeup() is often used in deserialization operations, such as re-establishing a database connection, or performing other initialization operations.

<?php
class Connection 
{
    protected $link;
    private $server, $username, $password, $db;
    
    public function __construct($server, $username, $password, $db)
    {
        $this->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(&#39;server&#39;, &#39;username&#39;, &#39;password&#39;, &#39;db&#39;);
    }
    
    public function __wakeup()
    {
        $this->connect();
    }
}
?>

The above is the detailed content of What are the magic methods of php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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