Home  >  Article  >  Backend Development  >  Serialized variables and serialized objects in PHP_PHP tutorial

Serialized variables and serialized objects in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:11:39730browse

  串行化大概就是把一些变量转化成为字符串的字节流的形式,这样比较容易传输、存储。当然,关是传输存储没有什么,关键是变成串的形式以后还能够转化回来,而且能够保持原来数据的结构。

  在PHP中有多串行化处理的函数:serialize(),该函数把任何变量值(除了资源变量)转化为字符串的形式,可以把字符串保存到文件里,或者注册为Session,乃至于使用curl来模拟GET/POST来传输变量,达到RPC的效果。

  如果要将串行化的变量转化成PHP原始的变量值,那么可以使用unserialize()函数。

  一、变量串行化

  我们举简单的例子来说明串行化,以及它的存储格式。

  整型:

  $var = 23;
  echo serialize($var);

  输出:

  i:23;

  浮点型:

  $var = 1.23;

  echo serialize($var);

  输出:

  d:1.229999999999999982236431605997495353221893310546875;

  字符串:

  $var = "This is a string";
  echo serialize($var);
  $var = "我是变量";
  echo serialize($var);

  输出:

  s:16:"This is a string";
  s:8:"我是变量";

  布尔型:

  $var = true;
  echo serialize($var);
  $var = false;
  echo serialize($var);

  输出:

  b:1;
  b:0;

  上面这些基本类型串行化之后的情况很清楚,串行化之后的存储格式是:

  变量类型:[变量长度:]变量值;

  就是第一位字符代表变量类型,第二个:代表分割,变量长度是可选的,就是在字符串类型里有,其他类型没有,最后一个就是变量值,每个串行化的值以";"作为结束。

  比如我们整型数字23串行化之后就是:i:23,那么它没有长度,只有类型和变量值,i代表integer,通过冒号分割,后面保存的是整型值23,包括浮点型(双字节型)也是一样。布尔型的话,类型是b(boolean),如果是true的话,那么串行化的值是1,如果是false那么值就是0。字

  符串值话中间会多一个保存的值得,保存字符串的长度值,比如字符串"This is a string",那么生成的串行化的值是 s:16:"This is a string"; s是string,代表类型,中间的16就是该字符串的长度,如果是中文的话,那么每个中文是两个字符来保存的,比如字符串 "我是变量",生成的串行化值是:s:8:"我是变量"; 就是8个字符的长度。

  下面我们重点来讲一下数组变量串行化。

  数组变量:

$var = array("abc", "def", "xyz", "123");
echo serialize($var);

  输出:

a:4:{i:0;s:3:"abc";i:1;s:3:"def";i:2;s:3:"xyz";i:3;s:3:"123";}

  就是把我的数组 $var 串行化得到的字符串值,我们的$var数组包括4个字符串元素,分别是"abc", "def", "xyz", "123",我们来分析一下串行化后的数据,为了简便起见,我们把串行化的数据列成数组的样式:

a:4:
{
i:0;s:3:"abc";
i:1;s:3:"def";
i:2;s:3:"xyz";
i:3;s:3:"123";
}

  这样排列就比较清晰了,看开始的字符串:a:4:{...} 首先第一个字符a保存的是变量类型是array(数组)类型,第二个 4 保存的是数组元素的个数,一共有4个,然后在{}之间数组元素的内容。比如第一个数组元素:i:0;s:3:"abc"; i代表是当前数组元素的索引值类型是整型,并且值是 0,元素值的类型是s(字符串的),个数是 3 个,具体值是"abc",分号结束,下面的数组元素依次类推。

  我们再看看使用字符串做为元素索引会如何:

$var = array("index1"=>"abc", "index2"=>"def", "index3"=>"xyz", "index4"=>"123");
echo serialize($var);

  输出:

a:4:{s:6:"index1";s:3:"abc";s:6:"index2";s:3:"def";s:6:"index3";s:3:"xyz";s:6:"index4";s:3:"123";}

  变成数组样式后:

a:4:
{
 s:6:"index1";s:3:"abc";
 s:6:"index2";s:3:"def";
 s:6:"index3";s:3:"xyz";
 s:6:"index4";s:3:"123";
}

  其实跟上面没有太大区别,不过是开始的索引变成了保存字符串的形式,比如第一个元素:s:6:"index1";s:3:"abc";第一项就是索引值:s:6:"index1"; s是类型,6是索引字符串的长度,"index1"就是索引的值。后面的s:3:"abc"; 就是元素值,这个好理解,就不讲了。

  从上面来看,我们大致了解了基本数据类型的串行化,其实我们完全可以构造自己的串行化功能,或者从这个角度去扩展,开发自己的串行化程序,便于我们的变量交换。

Of course, in fact, we can also use this function to serialize an array or any other variable into a string, and then use the curl function to simulate the GET/POST function to obtain data from the remote server without the user performing actions. .

2. Object serialization

Serialization of objects is also a relatively common function. It can serialize an object into a string, which can be saved or transmitted.

Let’s look at an example first:

class TestClass
{
 var $a;
 var $b;

function TestClass()
{
$this->a = "This is a";
$this->b = "This is b";
}

function getA()
{
return $this->a;
}

Function getB()
{
return $this->b;
}
}

$obj = new TestClass;
$str = serialize($obj);
echo $str;

Output result:

O:9:"TestClass":2:{s:1:"a";s:9:"This is a";s:1:"b";s:9:"This is b"; }

Let’s analyze the string after serialization of an object.

O:9:"TestClass":2:
{
 s:1:"a";s:9:"This is a";
 s:1:"b";s :9:"This is b";
}

First look at the content of the object itself: O:9:"TestClass":2:O means that this is an object type (object), then 9 means that the name of the object has been checked, and 2 means that the object has Several properties. Looking at the contents of two attributes:

 s:1:"a";s:9:"This is a"; In fact, it is similar to the content of the array. The first item: s:1:"a"; describes the attribute name, and the second item s:9:"This is a"; describes the attribute value. The following properties are similar.

First let’s talk about an application of object serialization. The following content is from the PHP manual, and the original text has not been changed.

Serialize() returns a string containing a byte stream representation of any value that can be stored in PHP. unserialize() can use this string to reconstruct the original variable value. Saving an object using serialization saves all variables in the object. The functions in the object are not saved, only the name of the class.

To be able to unserialize() an object, you need to define the class of the object. That is, if you serialize the object $a of class A in page1.php, you will get a string pointing to class A and containing the values ​​of all variables in $a. If you want to deserialize it in page2.php and reconstruct the object $a of class A, the definition of class A must appear in page2.php. This can be achieved, for example, by placing the definition of class A in an include file and including this file in both page1.php and page2.php.

<?php
// classa.inc:
class A
{
 var $one = 1;

function show_one()
{
echo $this->one;
}
}

// page1.php:
include("classa.inc");

$a = new A;
$s = serialize($a);
// Store $s somewhere so page2.php can find it
$fp = fopen("store" , "w");
fputs($fp, $s);
fclose($fp);

// page2.php:
// This line is needed for normal deserialization
include("classa.inc");

$s = implode("", @file("store"));
$a = unserialize($s);

// Now you can use the show_one() function of the $a object
$a->show_one();
?>

If you are using a session and use session_register() to register objects, these objects will be automatically serialized at the end of each PHP page and automatically deserialized on each subsequent page. Basically this means that once these objects become part of a session, they can appear on any page.

It is strongly recommended to include the definition of the classes of these registered objects in all pages, even if these classes are not used in all pages. If this is not done, and an object is deserialized without its class definition, it will lose its associated class and become an object of stdClass without any available functions at all, which is very useless.

So if $a becomes part of the session by running session_register("a") in the above example, the classa.inc file should be included in all pages, not just page1.php and page2.php.

Of course, serialized objects can actually be applied in many places. Of course, serialization is handled differently in PHP 5. Let’s take a look at what the manual says:

 serialize() checks whether there is a function with the magic name __sleep in the class. If so, the function will run before any serialization. It clears the object and should return an array containing the names of all variables in the object that should be serialized.

The purpose of using __sleep is to close any database connections the object may have, submit pending data, or perform similar cleanup tasks. In addition, this function is also useful if you have very large objects that do not need to be stored completely.

In contrast, unserialize() checks for the existence of a function with the magic name __wakeup. This function can reconstruct any resources the object may have, if present.

The purpose of using __wakeup is to re-establish any database connections that may have been lost during serialization and to handle other re-initialization tasks.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313909.htmlTechArticleSerialization is probably to convert some variables into a byte stream of strings, which is easier to transmit. storage. Of course, there is nothing to do with transmission and storage. The key is to change it into the form of a string...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn