What Is Serialization?

Serialization is the process of transforming the state information of an object into a form that can be stored or transmitted. During serialization, the object writes its current state to temporary or persistent storage. Later, you can recreate the object by reading or deserializing the state of the object from the store.

Serialization

1. Make customizations in some form of storage
* Binary serialization preserves type fidelity, which is useful for preserving the state of an object between different calls to an application. For example, objects can be shared between different applications by serializing them to the clipboard. You can serialize objects to streams, disks, memory, networks, and more.
Note: In PHP 3, objects lose class association during serialization and deserialization. The resulting variable is an object type, but has no classes or methods, so it is useless (like an array defined with a funny syntax).
serialize () returns a string containing a byte stream representation of any value that can be stored in PHP. unserialize () can use this string to reconstruct the original variable value. Using serialization to save an object saves all variables in the object. Functions in the object are not saved, only the name of the class.
To be able to unserialize () an object, you need to define the object's class. That is, if you serialize the object $ a of class A in page1.php, you will get a string pointing to class A and contain the values of all the variables in $ a. If you want to deserialize it in page2.php and reconstruct the object $ a of class A, the definition of class A must appear in page2.php. This can be achieved, for example, by placing the definition of class A in an include file and including this file in page1.php and page2.php.
<? php // classa.inc: class A {var $ one = 1; function show_one () {echo $ this-> one;}} / page1.php: include ("classa.inc"); $ a = new A; $ s = serialize ($ a); // Store $ s somewhere so page2.php can find $ fp = fopen ("store", "w"); fwrite ($ fp, $ s); fclose ($ fp); // page2.php: // This line is needed for normal deserialization include ("classa.inc"); $ s = implode ("", @file ("store")); $ a = unserialize ($ s); // Now you can use the show_one () function of the $ a object. $ a-> show_one ();?>
If you are using a session and used session_register () to register objects, these objects will be automatically serialized at the end of each php page and automatically deserialized on each subsequent page. Basically, these objects can appear on any page once they become part of the conversation.
It is strongly recommended that the definition of the classes of these registered objects be included in all pages, even if these classes are not used in all pages. If you don't do this, an object is deserialized without its class definition, it will lose its associated class and become an object of stdClass without any available functions at all, which is very useless.
So if $ a becomes part of the session by running session_register ("a") in the above example, you should include the classa.inc file on all pages, not just page1.php and page2.php.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?