PHP functions Serialize () and unserialize () usages, sprintf function _php Tutorial

Source: Internet
Author: User
Tags sprintf string back

PHP function serialize () and unserialize () Usage instance, sprintf function usage


This example describes the PHP function serialize () and unserialize () usage. Share to everyone for your reference. Here's how:

This example mainly describes the PHP function serialize () and the Unserialize () description and case. To change the serialized string back to PHP's value, use Unserialize (). Serialize () can handle any type except resource. You can even serialize () those arrays that contain pointers to their own references. The references in the array/object you are serialize () will also be stored.

Serialize () returns a string that contains a byte stream representing value that can be stored anywhere. This facilitates the storage or delivery of PHP values without losing their type and structure.

To change the serialized string back to PHP's value, use Unserialize (). Serialize () can handle any type except resource. You can even serialize () those arrays that contain pointers to their own references. The references in the array/object you are serialize () will also be stored.

When serializing an object, PHP will attempt to call the object's member function __sleep () before the sequence action. This allows the object to do any cleanup before it is serialized. Similarly, when you use Unserialize () to restore an object, the __wakeup () member function is called.

Note: In PHP 3, the object properties will be serialized, but the method will be lost. PHP 4 Breaks This limit and can store properties and methods at the same time. See the serialization Objects section of classes and objects for more information.
Serialize () and unserialize () are explained in the PHP manual as:

Serialize-generates a storable representation of a value
Serialize-produces a representation of a value that can be stored
Unserialize-creates a PHP value from a stored representation
unserialize-to create a PHP value from a stored representation
Serialize, translated to call "serial, make continuous", usually called it "serialization"

This function is very useful, especially with unserialize.
I think the more useful thing is to put the data into the database or record in the file when
Of course, this data must be more complex (not complex and need not serialize, I think at least an array), but also the database is not "index or primary key", of course, it is best that the database field in the system and any search program independent, Of course, after the serialize data is still able to search, because the specific data has not been encrypted or changed
Copy the Code code as follows: <?php
A little simpler.
$array = Array ();
$array [' key '] = ' website ';
$array [' Value ']= ' www.isoji.org ';
$a = serialize ($array);
echo $a;
Unset ($array);
$a = unserialize ($a);
Print_r ($a);

Declaration of a class
Class Dog {
var $name;
var $age;
var $owner;
Function Dog ($in _name= "unnamed", $in _age= "0", $in _owner= "Unknown") {
$this->name = $in _name;
$this->age = $in _age;
$this->owner = $in _owner;
}
function Getage () {
Return ($this->age * 365);
}

function GetOwner () {
Return ($this->owner);
}

function GetName () {
Return ($this->name);
}
}
Instantiation of this class
$ourfirstdog = new Dog ("Rover", "Lisa and Graham");
Convert this instance into a serialized string using the Serialize function
$dogdisc = serialize ($ourfirstdog);
Print $dogdisc; The $ourfirstdog has been serialized as a string o:3: "Dog": 3:{s:4: "Name"; s:5: "Rover"; s:3: "Age"; I:12;s:5: "Owner"; s:15: "Lisa and Graham";
print '
';
/*
Here you can store the string $dogdisc to anywhere like Session,cookie, database, PHP file
*/
We hereby unregister this class
Unset ($ourfirstdog);
/* Restore operation */
/*
Here you will read the string $dogdisc from where you stored it, such as Session,cookie, database, PHP file
*/

Here we use Unserialize () to restore an already serialized object
$pet = Unserialize ($dogdisc); The $pet at this point is already the $ourfirstdog object in front of you.
Get age and Name attributes
$old = $pet->getage ();
$name = $pet->getname ();
This class does not need to be instantiated at this time to continue to use, and the properties and values are persisted in the state before serialization
Print "Our first dog is called $name and are $old days old
";
print '
';
?>

I hope this article is helpful to everyone's PHP programming.


Write the PHP variable serialization and deserialization function, and 1 application examples

";p Rint_r (Unserialize ($new));? >serialize () encodes variables and their values into textual form
Unserialize () restore the original variable


PHP serialize () with the Unserialize () method of questioning, everyone?

$person 1 = new YSR (+ ' Yuanshao ');
$person 1 is an object that, after serializing it, just changes the way it behaves!
$person 2 = unserialize ($listInfo);
After restoring this object, it is an Object!
This object already has this getage () and GetName ()
How to do this, you can use these methods!

http://www.bkjia.com/PHPjc/907285.html www.bkjia.com true http://www.bkjia.com/PHPjc/907285.html techarticle PHP functions Serialize () and unserialize () usages, sprintf function Usage This example describes the PHP function serialize () and unserialize () usage. Share to everyone for your reference. Specific Party ...

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