Home > Article > Backend Development > Tutorial on how to serialize serialize objects in PHP
In actual project applications, some tasks cannot be completed in one or two pages. Since the variables are released after the script is executed, what we generated on this page The object ran into trouble when trying to use it on other pages.
If we need to pass the object and its methods to the page where we want to use the object, a relatively simple and feasible way is to serialize the object and store it or directly transfer it to the page where it is needed. Another way is to Registered as session variable.
Object serialization is to convert the object into a byte stream that can be stored. When we need to transmit an object over the network or write the object to a file or database, we need to serialize the object.
The complete process of serialization includes two steps: one is serialization, which is to convert the object into a binary string. The serialize() function is used to serialize an object; the other is deserialization, which is to convert the object into a binary string. The object is serialized into a binary string and then converted into an object. The unserialize() function is used to deserialize a serialized object. In this way, after the entire process, the type structure and data within the object are complete.
Grammar:
string serialize( mixed value ) mixed unserialize( string str [, string callback] )
Example:
name = $name; $this->age = $age; } function say() { echo "我的名字叫:".$this->name." "; echo " 我的年龄是:".$this->age; } } $p1 = new Person("张三", 20); $p1_string = serialize($p1); //将对象序列化后写入文件 $fh = fopen("p1.text", "w"); fwrite($fh, $p1_string); fclose($fh); ?>
Open the p1.text file, and the content written in it is as follows:
O:6:"Person":2:{s:12:" Person name";s:4:"张三";s:11:" Person age";i:20;}
But it is usually not parsed directly Characters generated by the above serialization.
Deserialization:
name = $name; $this->age = $age; } function say() { echo "我的名字叫:".$this->name." "; echo " 我的年龄是:".$this->age; } } $p2 = unserialize(file_get_contents("p1.text")); $p2 -> say(); ?>
Run this example, output:
我的名字叫:张三 我的年龄是:20
Because the serialized object cannot be serialized Its method, so when unserialize, the current file must contain the corresponding class or require the corresponding class file.
Serialization can only be used with limited users, because files need to be stored or written separately for each user, and the file name must not be repeated. In the case where the user cannot exit the browser normally, there is no guarantee that the file will be deleted.
When there are a large number of users, you can consider using session to save objects. For more information about sessions, see "PHP Session".
Example:
name = $name; $this->age = $age; } function say() { echo "我的名字叫:".$this->name." "; echo " 我的年龄是:".$this->age; } } $_SESSION["p1"] = new Person("张三", 20); ?>
Read session:
name = $name; $this->age = $age; } function say() { echo "我的名字叫:".$this->name." "; echo " 我的年龄是:".$this->age; } } $_SESSION["p1"] -> say(); ?>
Run this example, output:
我的名字叫:张三 我的年龄是:20
Same as serialization, when the registered object is a session variable Its methods cannot be saved, so when reading session variables, the current file must contain the corresponding class or the class file corresponding to require.
The above is the detailed content of Tutorial on how to serialize serialize objects in PHP. For more information, please follow other related articles on the PHP Chinese website!