_php tutorial for persistence under PHP

Source: Internet
Author: User
Tags object serialization
Persistence under PHP
The concept of "persistence" is the first time the author has come into contact with Java, and through this feature, the Application object can be transformed into a series of byte streams (called object serialization) to accommodate network transmission or preservation. Most wonderfully, the serialized object can also be reassembled to revert 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 machine on a Windows system can be correctly reassembled on a machine that is transmitted over the network to a Linux system. Persistence allows application objects to be protected from application run time--you can serialize an object, save it to disk, and assemble it again when you need it, to achieve a "lasting" effect.
The exciting fact is that PHP also supports this feature, and it starts with PHP3, which is implemented by serialize () and Unserialize (). In fact, the development environment such as ASP also implicitly supports this feature-saving an Application object in a session or Application object is a persistent performance, but unfortunately, the ASP does not explicitly provide this interface.
In PHP, variables of almost any type (this includes integer, Boolean, Float, array, and object) can be serialized. The reason for "almost" is that the only resource type does not support serialization, because the resource type in PHP is actually a pointer. As for the string type, because it is a byte stream in itself, there is no need to serialize it at all.
The usage of the Serialize () and unserialize () two functions are described below:
String serialize (mixed value): Returns the byte stream after value is serialized;
Mixed unserialize (String str): Returns the object after the STR is assembled.
Here are the application examples of these two functions:
class.inc.php file that holds the information for the class
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
";
echo "Username:". $this, Username. "
";
echo "Password:". $this, username. "
";
echo "Datetime:". $this, username. "
";
}
}
?>
login.php file for registering a new user
Importing class files
Require_once ("class.inc.php");
New Object
$user = new Userinfo ($_post[username], $_post[password], date ("Y-n-j h:i:s"));
Serializing objects
$user = Serialize ($user);
Writing 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);
?>
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, it is important to include the definition information for the class of the object in the "assemble" page, otherwise an error will occur. Of course, the upper is just for testing, in the actual application, in order to prevent the contents of the serialized object is changed, generally also to the byte stream "digital signature", at the time of assembly, and then the "signature" to prevent the object information is illegally tampered with.

http://www.bkjia.com/PHPjc/531996.html www.bkjia.com true http://www.bkjia.com/PHPjc/531996.html techarticle The concept of persistent "persistence" under PHP is the first time that I have come into contact with Java, and through this feature, you can convert an Application object into a series of byte streams (this is called ...).

  • 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.