简体   繁体   English

对象序列化/反序列化如何工作?

[英]How does object serialize/unserialize work?

I was reading about serialize/unserialize concepts of PHP. 我正在阅读关于PHP的序列化/反序列化概念。 I was wondering how they are stored in the filesystem/db. 我想知道它们是如何存储在filesystem / db中的。 I guess it is in the binary format. 我猜它是二进制格式。 However, I wonder how entire class is stored? 但是,我想知道整个班级是如何存储的? I understood that data in the data member can be stored but how are the methods stored? 我知道数据成员中的数据可以存储,但方法是如何存储的?

I mean, how does PHP know what code is written inside the function of say, someFunc() ? 我的意思是,PHP如何知道在someFunc()函数中编写了什么代码?

$obj = new ClassName();
$obj->someFunc();
$serial = serialize($obj);
$unserialobj = unserialize($serial);
$unserialobj->someFunc();

PHP can know what to do at line #2, but how it know what to do at line #5 which is an unserialized object? PHP可以知道第2行要做什么,但它知道如何在#5行中做一个非序列化的对象? Does it save the code as well? 它也保存代码吗?

When serializing an object, PHP only stores the object's current state, ie its property values. 序列化对象时,PHP仅存储对象的当前状态,即其属性值。 It does not serialize its methods. 它没有序列化它的方法。 The corresponding class needs to be loaded in memory at the time of unserialization. 在反序列化时,需要在内存中加载相应的类。 PHP will restore the state of the object from the serialized string and take the rest of the information (structure and methods) from the class of the same name. PHP将从序列化字符串中恢复对象的状态,并从同名的类中获取其余信息(结构和方法)。

PHP can know what to do at line#2 but how it knows what to do at line#5 which is a unserialized object? PHP可以知道在第2行做什么,但它知道如何在第5行做什么,这是一个非序列化的对象? does it save the code as well? 它也保存代码吗?

Yes, serialize() will save the information about the class which this object is an instance of, along with its state, so when you unserialize, you get an instance of that class, which in this case is ClassName . 是的, serialize()将保存有关此对象是其实例的类的信息及其状态,因此当您反序列化时,您将获得该类的实例,在本例中为ClassName

This is simple example to understand serialize and unserialize a object in php. 这是理解在php中序列化和反序列化对象的简单示例。 we covert object into string using serialization and use this object current status (with assign values) after unserialization on other page.. 我们使用序列化将对象转换为字符串,并在其他页面上反序列化后使用此对象的当前状态(带有赋值)。

c.php c.php

<?php class A {
      public $one ;

      public function A($val) {
          $this->one=$val;
         // echo $this->one;
      }

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

  }
  ?> 

c.php a file have class with name A . c.php文件的类名为A.
a.php a.php只会

<? 
require_once "c.php";

$ob= new A('by Pankaj Raghuwanshi : Object Searlization.');

$ob->display(); // Output is: by Pankaj Raghuwanshi : Object Searlization.

$s = serialize($ob);

// echo $s will show  a string of an object

?>
<br><A href='b.php?s=<?=$s;?>'>B-file</a>

We serialize this object convert into string and pass this string into another page by get method. 我们将此对象序列化转换为字符串,并通过get方法将此字符串传递到另一个页面。

Note : we can pass this string one page to another page with various method like using session, we can save into DB and fetch another page, save into text file. 注意:我们可以将这个字符串一页传递给另一个页面,使用各种方法,比如使用session,我们可以保存到DB中并获取另一个页面,保存到文本文件中。

We will unserialize this object on another file name is b.php 我们将在另一个文件名是b.php上反序列化此对象

b.php b.php

<? 
require_once "c.php";

$ob = unserialize($_GET[s]);
$ob->display();
// Output is: by Pankaj Raghuwanshi : Object Searlization.
?> 

after unserialization, object showing same behavior like a.php file and assign value of a.php still is in memory of object . 在反序列化之后,对象显示与a.php文件相同的行为并且分配a.php的值仍然在对象的内存中。 if we will unserialize this object after many http request . 如果我们将在许多http请求后反序列化此对象。 Object will persist all assign values in their memory. 对象将在其内存中保留所有赋值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM