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