php object serialization

伊谢尔伦
Release: 2016-11-23 11:08:31
Original
1159 people have browsed it

The so-called serialized object is to store the object in the session

All values ​​in PHP can be represented by using the function serialize() to return 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 object's variables, but it will not save the object's methods, only the class name.

In order to be able to unserialize() an object, the object’s class must have been defined. If you serialize an object of class A, a string related to class A will be returned that contains the values ​​of all variables in the object. 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

When an application uses the function session_register() to save objects into the session, these objects are automatically serialized at the end of each page and deserialized at the beginning of each page. So once the objects are saved in the session, they are available to pages throughout the application. However, the session_register() function has been deprecated in php5.3.0 and is no longer supported in php6.0.0, so do not rely on this function.

To serialize objects in your application for later use, it is highly recommended to include object class definitions throughout your application. Otherwise, it is possible that when deserializing an object, the definition of the class of the object is not found, and the class __PHP_Incomplete_Class_Name without a method is used as the class of the object, resulting in a useless object being returned.

So in the above example, when running session_register("a") and placing the variable $a in the session, the file classa.inc needs to be included in each page, instead of only the files page1.php and page2. php.


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!