Home > Backend Development > PHP Tutorial > [php classes and objects] Object serialization

[php classes and objects] Object serialization

不言
Release: 2023-03-24 10:14:02
Original
2766 people have browsed it

The content of this article is about [php classes and objects] object serialization, which has certain reference value. Now I share it with you. Friends in need can refer to it

Object sequence Serialize

Serialize object - store object in session

serialize() can serialize all values ​​in php to return a byte stream represented by a string.
The unserialize() function can change the string back to the original value of PHP.

Serializing an object will save all the variables of the object, but the methods of the object will not be saved, only the name of the class will be saved.

unserialize() object, the class of the object must be defined.
If you serialize an object of class A, a string related to class A and containing the values ​​of all variables of the object will be returned.
If you want to deserialize an object in another file, the object's class must be defined before deserializing, either by including a file that defines the class or by using the function spl_autoload_register().

<?php// classa.inc:

  class A {
      public $one = 1;      public function show_one() {
          echo $this->one;
      }
  }// page1.php:

  include("classa.inc");  $a = new A;  $s = serialize($a);  // 把变量$s保存起来以便文件page2.php能够读到
  file_put_contents(&#39;store&#39;, $s);// page2.php:

  // 要正确了解序列化,必须包含下面一个文件
  include("classa.inc");  $s = file_get_contents(&#39;store&#39;);  $a = unserialize($s);  // 现在可以使用对象$a里面的函数 show_one()
  $a->show_one();?>
Copy after login
Copy after login

Use the function session_register() to save objects to the session. These objects will be automatically serialized at the end of each page, and automatically deserialized at the beginning of each page. So once the objects are saved in the session, they are available to pages throughout the application.

You can use the __sleep() and __wakeup() methods on the object to handle serialization/deserialization events.

session_register() Deprecated in PHP 5.3.0 and removed in PHP 5.4.0.

Object serialization

Serialize objects - store objects in the session

serialize() can serialize all php The value inside is returned as a string containing a byte stream.
The unserialize() function can change the string back to the original value of PHP.

Serializing an object will save all the variables of the object, but the methods of the object will not be saved, only the name of the class will be saved.

unserialize() object, the class of the object must be defined.
If you serialize an object of class A, a string related to class A and containing the values ​​of all variables of the object will be returned.
If you want to deserialize an object in another file, the object's class must be defined before deserializing, either by including a file that defines the class or by using the function spl_autoload_register().

<?php// classa.inc:

  class A {
      public $one = 1;      public function show_one() {
          echo $this->one;
      }
  }// page1.php:

  include("classa.inc");  $a = new A;  $s = serialize($a);  // 把变量$s保存起来以便文件page2.php能够读到
  file_put_contents(&#39;store&#39;, $s);// page2.php:

  // 要正确了解序列化,必须包含下面一个文件
  include("classa.inc");  $s = file_get_contents(&#39;store&#39;);  $a = unserialize($s);  // 现在可以使用对象$a里面的函数 show_one()
  $a->show_one();?>
Copy after login
Copy after login

Use the function session_register() to save objects to the session. These objects will be automatically serialized at the end of each page, and automatically deserialized at the beginning of each page. So once the objects are saved in the session, they are available to pages throughout the application.

You can use the __sleep() and __wakeup() methods on the object to handle serialization/deserialization events.

session_register() Deprecated in PHP 5.3.0 and removed in PHP 5.4.0.

Related recommendations:

[php classes and objects] objects and references

[php classes and objects] trait

[php classes and objects]Final keyword

The above is the detailed content of [php classes and objects] Object serialization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template