Tutorial on how to serialize serialize objects in PHP

巴扎黑
Release: 2023-03-14 20:00:01
Original
2276 people have browsed it

Storage and Transmission of Objects

In actual project applications, some tasks cannot be completed in one or two pages. Since the variables are released after the script is executed, what we generated on this page The object ran into trouble when trying to use it on other pages.

If we need to pass the object and its methods to the page where we want to use the object, a relatively simple and feasible way is to serialize the object and store it or directly transfer it to the page where it is needed. Another way is to Registered as session variable.

Tutorial on how to serialize serialize objects in PHPSerialized object

Object serialization is to convert the object into a byte stream that can be stored. When we need to transmit an object over the network or write the object to a file or database, we need to serialize the object.

The complete process of serialization includes two steps: one is serialization, which is to convert the object into a binary string. The serialize() function is used to serialize an object; the other is deserialization, which is to convert the object into a binary string. The object is serialized into a binary string and then converted into an object. The unserialize() function is used to deserialize a serialized object. In this way, after the entire process, the type structure and data within the object are complete.

Grammar:

string serialize( mixed value )
mixed unserialize( string str [, string callback] )
Copy after login

Example:

name = $name;
        $this->age = $age;
    }

    function say() {
	echo "我的名字叫:".$this->name."
";
	echo " 我的年龄是:".$this->age;
    }
}

$p1 = new Person("张三", 20);
$p1_string = serialize($p1);

//将对象序列化后写入文件
$fh = fopen("p1.text", "w");
fwrite($fh, $p1_string);
fclose($fh);
?>
Copy after login

Open the p1.text file, and the content written in it is as follows:

O:6:"Person":2:{s:12:" Person name";s:4:"张三";s:11:" Person age";i:20;}
Copy after login

But it is usually not parsed directly Characters generated by the above serialization.

Deserialization:

name = $name;
        $this->age = $age;
    }

    function say() {
	echo "我的名字叫:".$this->name."
";
	echo " 我的年龄是:".$this->age;
    }
}

$p2 = unserialize(file_get_contents("p1.text"));
$p2 -> say();
?>
Copy after login

Run this example, output:

我的名字叫:张三
我的年龄是:20
Copy after login
Copy after login
Prompt
  1. Because the serialized object cannot be serialized Its method, so when unserialize, the current file must contain the corresponding class or require the corresponding class file.

  2. Serialization can only be used with limited users, because files need to be stored or written separately for each user, and the file name must not be repeated. In the case where the user cannot exit the browser normally, there is no guarantee that the file will be deleted.

    Tutorial on how to serialize serialize objects in PHPObjects are registered as session variables

    When there are a large number of users, you can consider using session to save objects. For more information about sessions, see "PHP Session".

    Example:

    name = $name;
            $this->age = $age;
        }
    
        function say() {
    	echo "我的名字叫:".$this->name."
    ";
    	echo " 我的年龄是:".$this->age;
        }
    }
    
    $_SESSION["p1"] = new Person("张三", 20);
    ?>
    Copy after login

    Read session:

    name = $name;
            $this->age = $age;
        }
    
        function say() {
    	echo "我的名字叫:".$this->name."
    ";
    	echo " 我的年龄是:".$this->age;
        }
    }
    
    $_SESSION["p1"] -> say();
    ?>
    Copy after login

    Run this example, output:

    我的名字叫:张三
    我的年龄是:20
    Copy after login
    Copy after login

    Same as serialization, when the registered object is a session variable Its methods cannot be saved, so when reading session variables, the current file must contain the corresponding class or the class file corresponding to require.

The above is the detailed content of Tutorial on how to serialize serialize objects in PHP. 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