Implement persistence in PHP

Source: Internet
Author: User
Tags object serialization
Implementation of persistence in PHP implementation of persistence in PHP <br/> The concept of "persistence" was first introduced by the author in Java. through this feature, applications can be persistently stored 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.