All values in PHP can be represented by using the function serialize () to return a string containing a stream of bytes. The unserialize () function can re-change the string back to the original PHP value. Serializing an object saves all variables of the object, but does not save the object's methods, only the name of the class is saved.
To be able to Unserialize () an object, the class of the object must already be defined. If an object of Class A is serialized, it returns a string that is related to Class A and contains all the variable values of the object. If you want to deserialize an object in another file, the class of the object must be defined before the solution is serialized, either by including a file that defines the class or by using the function spl_autoload_register () .
<?php
Classa.inc:
classA{
Public$one= 1;
Public FunctionShow_one() {
Echo$this - One;
}
}
//page1.php:
include ("Classa.inc");
$a= newA;
$s= Serialize($a);
//Save the variable $s so that the file page2.php can read
file_put_contents(' Store ', $s);
//page2.php:
//To properly understand serialization, you must include one of the following files
include ("Classa.inc");
$s= file_get_contents(' Store ');
$a= unserialize($s);
//You can now use the function Show_one () inside the object $ A
$a -Show_one();
?>
When an application uses the function Session_register () to save an object to the session, the objects are automatically serialized at the end of each page, and are automatically deserialized at the beginning of each page. So once the object is saved in the session, the entire application's page can use those objects. However,Session_register () was removed after php5.4.0.
Serializing an object in an application for later use strongly recommends that the entire application contain the definition of the object's class. Otherwise, there is a possibility that when the object is being deserialized, no definition of the object's class is found, and the class __php_incomplete_class_name without the method is used as the object's class, resulting in an unused object being returned.
So in the above example, when you run session_register ("a") , the variable $a is placed in the session, You need to include the file classa.inc on each page, not just the file page1.php and
This article is from the "Half City" blog, please be sure to keep this source http://vabc1314.blog.51cto.com/2164199/1834976
PHP serialization objects-storing objects in a session