Home Backend Development PHP Tutorial Serialized variables and serialized objects in PHP_PHP tutorial

Serialized variables and serialized objects in PHP_PHP tutorial

Jul 21, 2016 pm 04:11 PM
php variable and string byte object Serialization become of Convert

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

  在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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles