Implement persistence in PHP
The concept of "persistence" was first introduced by the author in Java. Through this feature, application objects can be converted into a series of byte streams (called object serialization ), to adapt to network transmission or storage. The most amazing thing is that serialized objects can also be re-assembled and restored to their previous appearances. This means that this mechanism can automatically compensate for differences between operating systems. In other words, an object serialized on a Windows machine can be transmitted over the network to a Linux machine for correct re-assembly. "Persistence" can prevent application objects from being limited by the running time of the application-you can serialize an object and save it to the disk for assembly when necessary again, it can achieve a "lasting" effect.
It is exciting that PHP also supports this feature, and it has been supported since PHP3. It is implemented through the Serialize () and Unserialize () functions. In fact, a development environment like ASP also implicitly supports this feature-saving an Application object in a Session or Application object is a persistence, but unfortunately, ASP does not explicitly provide this interface.
In PHP, variables of almost any type (including Integer, Boolean, Float, Array, and Object) can be serialized. The reason why "almost" is because only the Resource type does not support serialization is because the Resource type in PHP is actually a pointer. As for the String type, it is a byte stream, so there is no need for serialization.
The following describes the usage of the Serialize () and Unserialize () functions:
String serialize (mixed value): returns the byte stream after the value is serialized;
Mixed unserialize (string str): returns the object after str is assembled.
The following are examples of the two functions:
// Class. inc. php file, used to save class information
// User information used for testing
Class Userinfo
{
Var $ username;
Var $ password;
Var $ datetime;
Function Userinfo ($ username, $ password, $ datatime)
{
$ This-> username = $ username;
$ This-> password = $ password;
$ This-> datetime = $ datetime;
}
Function output ()
{
Echo "User Information->
";
Echo "Username:". $ this-> username ."
";
Echo "Password:". $ this-> username ."
";
Echo "Datetime:". $ this-> username ."
";
}
}
?>
// Login. php file, used to register a new user
// Import a class file
Require_once ("class. inc. php ");
// Create an object
$ User = new Userinfo ($ _ POST [username], $ _ POST [password], date ("Y-n-j H: I: s "));
// Serialize the object
$ User = Serialize ($ user );
// Write the object to the local database
$ Con = mysql_connect ();
Mysql_select_db ("test ");
Mysql_query ($ con, "insert into testTable (id, userinfo) VALUES (1, $ user )");
Mysql_close ($ con );
?>
// Userinfo. php file, used to display user information
Require_once ("class. inc. php ");
// Retrieve the object from the database
$ Con = mysql_connect ();
Mysql_select_db ("test ");
$ Result = mysql_query ($ con, "SELECT * FROM testTable WHERE id = 1 ");
$ Record = mysql_fetch_assoc ($ result );
$ User = Unserialize ($ record [userinfo]);
// Output user information
$ User-> output ();
Mysql_free ($ result );
Mysql_close ($ con );
?>
In object serialization, the most important thing is that the "assembly" Page must contain the definition information of the class of the object. Otherwise, an error occurs. Of course, the above is only used for testing. In actual applications, in order to prevent the content of the serialized object from being changed, a "Digital Signature" is usually required for the byte stream. During assembly, then, the "signature" is verified to prevent illegal tampering of the object information.