Home > Backend Development > PHP Tutorial > What does php __sleep method mean?

What does php __sleep method mean?

青灯夜游
Release: 2023-04-09 18:40:02
Original
3831 people have browsed it

In PHP, "__sleep()" is a magic method. When executing serialize(), this method will be called first; it can be used to clean up the object and return an object containing all the objects that should be serialized. An array of variable names. The "__sleep()" method is often used to submit uncommitted data, or similar cleanup operations.

What does php __sleep method mean?

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

__sleep() in PHP Detailed method explanation

__sleep(), when executing serialize(), this function will be called first

serialize() function will check whether There is a magic method __sleep(). If it exists, this method will be called first, and then the serialization operation will be performed.

This function can be used to clean the 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 generated.

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.

Function:

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

Please refer to the following code for details:

<?php
class Person
{
    public $sex;
    public $name;
    public $age;
    public function __construct($name="",  $age=25, $sex=&#39;男&#39;)
    {
        $this->name = $name;
        $this->age  = $age;
        $this->sex  = $sex;
    }
    /**
     * @return array
     */
    public function __sleep() {
        echo "当在类外部使用serialize()时会调用这里的__sleep()方法<br>";
        $this->name = base64_encode($this->name);
        return array(&#39;name&#39;, &#39;age&#39;); // 这里必须返回一个数值,里边的元素表示返回的属性名称
    }
}
$person = new Person(&#39;小明&#39;); // 初始赋值
echo serialize($person);
echo &#39;<br/>&#39;;
Copy after login

Code running results:

当在类外部使用serialize()时会调用这里的__sleep()方法
O:6:"Person":2:{s:4:"name";s:8:"5bCP5piO";s:3:"age";i:25;}
Copy after login

Extended knowledge:

Magic method

When exploiting PHP deserialization, it is often necessary to use the magic method in deserialization to check whether there are sensitive operations in the method for exploitation.

PHP reserves all class methods starting with __ (two underscores) as magic methods.

Common magic methods

__construct()//Triggered when the object is created

__destruct() //Triggered when the object is destroyed

__call() //Triggered when an inaccessible method is called in the object context

__callStatic() //Triggered when an inaccessible method is called in the static context

__get() // Used to read data from inaccessible properties

__set() // Used to write data to inaccessible properties

__isset() // On inaccessible properties Triggered by calling isset() or empty()

__unset() // Triggered when unset() is used on an inaccessible property

__invoke() // When a script attempts to call an object as Triggered when function

__sleep() //This function can be used to clean up the object and return an array containing the names of all variables in the object that should be serialized.

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

php magic method official documentation: https://www.php.net/manual/zh/language.oop5.magic.php

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of What does php __sleep method mean?. 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