Implement Persistence in PHP

Source: Internet
Author: User
Tags object datetime functions implement object serialization php file resource string
Implement Persistence in PHP

The concept of "persistence" is the author's first exposure in Java, through which the Application object can be transformed into a series of byte streams (called object serialization) to accommodate network transmission or preservation. The most fascinating thing is that the serialized object can also be reassembled and restored to its previous appearance. This means that the mechanism can automatically compensate for differences between operating systems. In other words, an object that is serialized on a Windows system machine can be transferred over the network to the machine on a Linux system and is correctly reassembled. Persistence enables application objects to be kept out of the application's running time--you can serialize an object, then save it to disk, and assemble it again when it is needed to achieve a "lasting" effect.

It's exciting that PHP also supports this feature, and it's supported from PHP3, and it's done through the functions of serialize () and Unserialize (). In fact, a development environment like ASP implicitly supports this feature-saving an Application object in a session or Application object is a persistent manifestation, but unfortunately the ASP does not explicitly provide this interface.

In PHP, variables of almost any type (including integers, Boolean, Float, array, and object) can be serialized. "Almost" is because the resource type does not support serialization, simply because the resource type in PHP is actually a pointer. As for the string type, because it is a byte stream itself, there is no need for serialization at all.

The following describes the use of the Serialize () and unserialize () two functions:

String serialize (mixed value): Returns the byte stream after the value is serialized;
Mixed unserialize (String str): Returns the object after which STR is assembled.

The following is an example of the application of these two functions:

<?php
class.inc.php file, for saving class information

User Information classes 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-> <br>";
echo "Username:". $this-> Username. " <br> ";
echo "Password:". $this-> username. " <br> ";
echo "Datetime:" $this-> username. " <br> ";
}
}
?>

<?php
login.php files, for registering new users

Import class Files
Require_once ("class.inc.php");

New Object
$user = new Userinfo ($_post[' username '], $_post[' password '], date ("Y-n-j h:i:s"));
Serializing an Object
$user = Serialize ($user);

To write an 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);
?>

<?php
userinfo.php file, for displaying user information

Require_once ("class.inc.php");

Fetching objects 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 to include the definition information for the class of the object in the "Assembly" page, otherwise an error occurs. Of course, the above is only used for testing, in the actual application, in order to prevent the serialization of the contents of the object is changed, generally also to the byte stream "digital signature", in the assembly, and then "signature" verification to prevent the object information is illegally tampered.



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.