php-Object Serialization ____php

Source: Internet
Author: User

Serialization (serialized)

1. Turn objects into strings (do not read this string)---serialization

2. Process of returning a string to an object---drag

Note (serialization time):

1. Transfer objects to the network

2. Persist the object because in memory, as the script executes, the class object is purged from memory



Serialization of Class objects

$p = new Person ();

Serializing an Object

$str = serialize ($p);

To save a string in a file Objstr.txt

File_put_contents ("Objstr.txt", $str);

echo "object to end string, save to file success ...";


Reads a string from a file and then drag the string.

$str = file_get_contents ("Objstr.txt");

$obj = Unserialize ($STR);


On the Magic method of serialization

__sleep () method, when serialized, the method that is automatically invoked

Function: You can set the properties of the object that needs to be serialized, that is, the partial serialization of the object to be serialized

1. As long as in this method, returns an array, declared in the array that attribute name, which attribute will be serialized, not in this array, will not be serialized. By default this method, all attributes are serialized.


__wakeup () The method that is invoked automatically when drag is in line

Function: Object serialized back object, initialized, and __construct (), __clone function similar


function __sleep () {

echo "Serialization only name,age<br/>";

Return Array (' name ', ' age ');//indicates serialization only for Name,age

}

function __wakeup () {

echo "Drag, automatically invoke wakeup this method";

$this->age = 12;

}





Serialization of data JSON---javascript object

<script>

var obj = {

Name: "John",

Age:10,

Sex: "Male",

Address: {

Email: "Xx@163.com"

}

};

alert (obj.name);

</script>


$arr = Array ("name" => "Zhangsan", "Age" =>10, "sex" => "male");

Serialization of Array JSON

$str = Json_encode ($arr);//Generate standard JSON string {"name": "Tianshanshan", "Age": 23} This is the standard JSON string in which the key is quoted.

Echo $str;

Drag of the line

$pobj = Json_decode ($STR);//Go to Object

Var_dump ($parr);

The second argument is true, then drag will become an array.

$parr = Json_decode ($str, true);//go to an array

echo $parr ["name"];


Eval () function---Check and execute PHP code

$STR = "echo ' abc ';";

Echo $str; This outputs the echo ' ABC ';

eval ($STR);//Here Check $str is not a legitimate PHP code, if legitimate, to execute this code, the result will be output ABC


Var_dump (); Output Print format form

$arr = Array ("One" =>1, "two" = "222", "three" =>333);

Var_dump ($arr);


Var_export ();

Var_export ($arr);//output Print format Confidence, is a standard PHP syntax data

$a = Var_export ($arr, true);//does not output, returns a string assignment to $a;

$b = eval (' $b = '. Var_export ($arr, true). ";");



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.