Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between SERIALIZE and JSON serialization and deserialization in PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what are the differences between SERIALIZE and JSON serialization and deserialization operations in PHP, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The details are as follows:

What is the difference between serialization and deserialization of SERIALIZE and JSON in PHP? we can take a look at this problem with the editor. The details of the operation are shown below.

In PHP, what's the difference between serialize and json serializing or deserializing an object or array?

Suppose an object and an array:

$web = new stdClass;$web- > site = 'tantengvip';$web- > owner =' tuntun';$web- > age = 5 web / and $web = array (); $web ['site'] =' tantengvip';$web ['owner'] =' tuntun';$web ['age'] = 5

Serialize and deserialize them with the serialize function and the unserialize function, respectively, and see what the print result is, as follows:

Use the serialize method:

Var_dump (serialize ($web)); var_dump (unserialize (serialize ($web); var_dump (json_encode ($web)); var_dump (json_decode (json_encode ($web)

Results:

String'Otantengvip 8: "stdClass": 3: {SRAV 4: "site"; SRAV 10: "tantengvip"; SRAV 5: "owner"; SRAV 6: "tuntun"; SRAV 3: "age"; iRIA 5 }'(length=87) object (stdClass) [127] public 'site' = > string' tantengvip' (length=10) public 'owner' = > string' tuntun' (length=6) public 'age' = > int 5string' {"site": "tantengvip", "owner": "tuntun", "age": 5}'(length=46) object (stdClass) [127] public 'site' = > string' tantengvip' (length=10) public 'owner' = > string' tuntun' (length=6) public 'age' = > age' 5

Use the json method:

Var_dump (serialize ($web)); var_dump (unserialize (serialize ($web); var_dump (json_encode ($web)); var_dump (json_encode ($web), true))

Results:

String'aowner 3: {string 4: "tantengvip"; SRAV 10: "tantengvip"; SRAV 5: "owner"; SRAV 6: "tuntun"; SRAV 3: "age"; iRV 5 }'(length=74) array (size=3) 'site' = > string' tantengvip' (length=10) 'owner' = > string' tuntun' (length=6) 'age' = > int 5string' {"site": "tantengvip", "owner": "tuntun", "age": 5}'(length=46) array (size=3) 'site' = > string' tantengvip' (length=10) 'owner' = > string' tuntun' (length=6) 'age' = > int 5

We found that for such an object or array defined earlier, using serialize and json for serialization, the result of deserialization is the same as the original, except that the format of the serialization is different.

So what's the difference between them? The following text summary is very good, do not explain it yourself, you can write code to verify.

Serialization and deserialization using json

Advantages:

Variables can still be read after serialization

Can be used by other systems because the JSON format is standard

Disadvantages:

Valid only for UFT-8 data, other encodings may not work well

Valid only for examples of the stdClass class

Serialization and deserialization using serialize

Advantages:

Allow variables that are not UTF-8

Support for other instances with the exception of stdClass

Disadvantages:

The encoded text is unreadable to people.

Cannot be referenced by systems in other languages

Okay, write a code to see:

Class Test {private $pri = 'pri'; public $class =' Test'; public function _ construct () {$this- > class = 'Test construct'; $this- > pri =' pri construct';}} $test = new Test (); var_dump (serialize ($test)); var_dump (unserialize (serialize ($test)); var_dump (json_encode ($test)); var_dump (json_decode (json_encode ($test)

Results:

String'Opri construct 4: "Test": 2: {Test pri "; SVR 13:" pri construct "; SVR 5:" class "; SSV 14:" Test construct ";}'(length=86) object (Test) [127] private 'pri' = > string' pri construct' (length=13) public 'class' = > string' Test construct' (length=14) string'{" class ":" Test construct "}'(length=26) object (stdClass) [127] public 'class' = > string'Test construct' (length=14)

We found that json serialization and deserialization lost private member variables in the class, while serialize serialization and deserialization were fine as long as they were variables of the class, but the member methods of the class could not serialize and deserialize.

In general, it is better to use json, because json is a common format across platforms, and in addition to json, xml is also better. So when do you use serialize?

The magic method _ _ wakeUp () is called by default when a class is deserialized by serialize, which allows the object to re-establish various states that cannot be preserved during serialization. For example: database connection and so on. That's another question. I don't want to dig deeper here.

These are all the contents of the article "what are the differences between SERIALIZE and JSON serialization and deserialization in PHP?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report