Home  >  Article  >  Backend Development  >  Why does php need to serialize?

Why does php need to serialize?

(*-*)浩
(*-*)浩Original
2019-09-26 11:11:402843browse

Serialization is the process of converting objects into a format that is easy to transmit. Generally, they are converted into stream files and placed into memory or IO files.

Why does php need to serialize?

For example, you can serialize an object and then transfer the object over the Internet between a client and a server using HTTP , or share it with other applications. On the contrary, deserialization reconstructs the object according to the stream.

PHP serialization: (Recommended learning: PHP programming from entry to proficiency)

string serialize ( mixed $value )

Serialization is to convert variables or objects into characters stringing process.

During serialization, the system will first call the __sleep() magic method, and then filter the data inside, just like going out to pick out the clothes to bring. The default is all the attributes of an object, and then Then call the serialize() function like this:

<?php
class Animal{
public $name;
public $age;
public $height;
function __construct($name,$age,$heigh){
$this->name=$name;
$this->age=$age;
$this->height=$heigh;
 
}
function __sleep(){
$this->name="小白猫";
return [&#39;name&#39;,&#39;age&#39;,];
 
}
function __wakeup(){
 
}
}
$cat=new Animal("小花猫",5,20);
var_dump(serialize($cat));
 
?>

In the above example, the final output serialization content is like this:

Why does php need to serialize?

Serialization The relationship between the process and the callback function: The concept of the callback function is that the parameter of one function is the return value of another function, and during serialization, the parameter of the serialize() function is the return value of the __sleep() function, so The serialization process is the execution process of a callback function.

The above is the detailed content of Why does php need to serialize?. 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