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

[php classes and objects] Object serialization

Apr 18, 2018 pm 01:47 PM
php object Serialization

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().

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?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('store', $s);// page2.php:

 

  // 要正确了解序列化,必须包含下面一个文件

  include("classa.inc");  $s = file_get_contents('store');  $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().

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?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('store', $s);// page2.php:

 

  // 要正确了解序列化,必须包含下面一个文件

  include("classa.inc");  $s = file_get_contents('store');  $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!

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

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 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

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

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles