33

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()?

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
JasonDavis
  • 48,204
  • 100
  • 318
  • 537

4 Answers4

27

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

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
  • 4
    you just said _you to keep an array in a text form_ which is wrong. You can serialize any type of variable. you can serialize even an object. the only thing you need consider when you are *unserializing* an object is to have the declaration of the object you will unserialize – Gabriel Sosa Jul 25 '09 at 02:32
  • 1
    BTW, At the end of the paragraph I originally put that it was used for both objects and arrays. – Tyler Carter Jul 25 '09 at 02:38
19

I often see seralized data store in Database, and I really don't like this :

  • it's really hard to work in SQL with that data : how do you write conditions on serialized data ? Even harder : how do you update it ? Do you really write a PHP script that fetches every lines, unserialize those, modifies them, re-serialize, and stores them back in DB ? :-(
  • the day you will want to migrate your data to another software, it'll require more work to migrate the data (and be even more work if the new software is not written in PHP, btw)

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 ^^

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 2
    Awesome comment Pascal! If people are interested, there is also this awesome [post](http://stackoverflow.com/questions/804045/preferred-method-to-store-php-arrays-json-encode-vs-serialize) that compares json_encode and serialize. – Mick Jun 22 '12 at 01:14
7

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.

James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • 1
    Yep, this is generally how I use it. Especially if you have an unknown number of key=>val pairs (with unknown names), its hard to build a good db structure to handle that. – Christian Jul 24 '09 at 23:31
  • 2
    @ Christian: Look at the EAV pattern. It is not difficult at all, and in fact is much more flexible, to normalize this kind of data into a database. –  Jul 24 '09 at 23:44
  • 2
    I started using json for this. allow you to save the data on a *more* standar format than the php serialization – Gabriel Sosa Jul 25 '09 at 02:33
  • @Nelson: Interesting, will have a look into the EAV pattern in more depth. Thanks for letting me know! – Christian Jul 26 '09 at 03:47
0

Serialization is used when you want to pass data outside PHP, for example to Javascript.

Yeah, it often works to send strings, integers and strings as they are, but not more complex structures like arrays. Read more about passing a PHP array to Javascript here.

You can also format your data as JSON that is widely supported in many languages, but then you loose PHP native types and your own classes when converting to it. Read more about JSON and PHP here.

Community
  • 1
  • 1
Andrey Prokhorov
  • 890
  • 2
  • 13
  • 25
  • You start you answer with the info that serialization is used to pass data outside of php to e.g. js, but what is the purpose to keep the native php types if your target isn't php anymore? – t.niese Mar 12 '16 at 13:16
  • Good comment! You are right, if your target isn't php anymore then it probably does not support PHP native types. But your middle-step (target) can be to store data in a database and then use it again in PHP. – Andrey Prokhorov Mar 12 '16 at 13:30