This article illustrates the method of PHP serialization operation. Share to everyone for your reference, specific as follows:
Serialization is the conversion of variable data to a string (unlike a type conversion mechanism), which is typically used to store data (files) and then revert (deserialize) in other cases
Serialization:
$val = serialize ($var);
File_put_contents ('./*.txt ', $val);
Deserialization:
$val = file_get_contents ('./*.txt ');
$var = Unserialize ($val);
When an object is serialized, the __sleep () in the class is automatically invoked.
When deserializing an object, the __wakeup () of the corresponding class is automatically invoked, and the corresponding class declaration must be available in the reverse sequence operation.
In __sleep () , you can perform a cleanup shutdown of some data (resources) and return an array that stores the properties of the objects that need to be serialized, that is, you can select attributes for serialization.
Class bee{public
$a;
protected $b;
Private $c;
Public Function __sleep () {return
array (' A ', ' B ');
}
__wakeup () some useful ' data recovery work ' when doing deserialization operations, such as connecting to a database
Public Function __wakeup () {
$this->func ();
}
__tostring ()
This method is automatically invoked when an object is treated as a string. This method allows you to return the appropriate string, and an error occurs if the method is not defined.
Class bee{public
function __tostring () {
echo ' string ';
}
}
$obj = new Bee ();
Object cannot be used directly when the string uses the
echo $obj;
__invoke ()
This method is automatically invoked when an object is used as a method
Class bee{public
function __invoke () {
echo ' invoke ';
}
}
$obj = new Bee ();
Object cannot be used directly as a string using the
echo $obj ();
For more information about PHP interested readers can view the site topics: "PHP string (String) Usage summary", "PHP array Operation techniques Encyclopedia", "PHP basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP Network programming Skills Summary" , "Introduction to PHP object-oriented Programming", "PHP+MYSQL database operations to get Started" and "PHP common database Operation Skills Summary"
I hope this article will help you with your PHP programming.