I just learned about the serialize()
andunserialize()
functions. What are some uses for this? I know people serialize things to put into a database. Could you give me some example uses where it is helpful?
I also see serialized code in javascript, is this the same? Can a serialized string in javascript can be unserialized with php unserialize()
?
The serialize array function is a built-in function in PHP. The serialization of data means converts a value into a sequence of bits to be stored in a memory buffer, in a file, or transfer across a network. The array is complex data types; we can not see its content directly.
The process whereby an object or data structure is translated into a format suitable for transfer over a network, or storage (e.g. in an array buffer or file format). In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() .
Serialization and deserialization work together to transform/recreate data objects to/from a portable format. Serialization enables us to save the state of an object and recreate the object in a new location. Serialization encompasses both the storage of the object and exchange of data.
Serialization in Java is the concept of representing an object's state as a byte stream. The byte stream has all the information about the object. Usually used in Hibernate, JMS, JPA, and EJB, serialization in Java helps transport the code from one JVM to another and then de-serialize it there.
PHP serialize allows you to keep an array or object in a text form. When assigning arrays to things like $_SESSION, it allows PHP to store it in a text file, and then recreate it later. Serialize is used like this for objects and variables. (Just make sure you have declared the class the object uses beforehand)
Wordpress on the other hand uses it for a very similar method, by storing the serialized arrays directly in a database. If you keep a database of keys => values, this could be very beneficial because of the flexibility of arrays, you can store anything in the value parameter.
And heres the link (courtesy of first commentor): http://us3.php.net/serialize
I often see seralized data store in Database, and I really don't like this :
Still, I admit it is an easy way to store not-well-defined data... and I do sometimes use it for that...
Another use for serialization is to facilitate data exchange between two systems : sending objects through some kind of webservice, for instance, requires them to be serialized in some kind of way.
If the two systems are PHP, you could envisage using serialize
/unserialize
. But, here too, what if one of the system is not PHP anymore ? Using JSON or SOAP is probably a better choice : a bit harder at first, but probably a more long-term solution, as those formats are known in other languages too.
One thing I use PHP's serialize
function is to store data in cache (like APC's user's cache), in a PHP application : you cannot store objects as-is : you have to serialize them. As the cache is used only by one application, it is not necessary to use a format known by many languages ; so, serialize is OK... And, to store data in cache, you should use a really fast serialization function -- and serialize is pretty fast ^^
I often use serialize
to store important information in the database that isn't worth creating a whole new field for, but could be of use in the future.
For instance, if the user fills out a form that I only store a few elements from, but I want to keep them all in case I need them later, I'll serialize the array of form elements and store it in the database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With