Sunday, October 19, 2014

Don't be unfair with serialize() !

Hello my friends ,

Today I want to talk about Serialization .

Most of us as developers doesn't interested in serialization concept within developing process , we can say that serialization is the process of translating data structures or object state into a format that can be stored.

Others attack serialization it self 'IF WE CAN CREATE EXCELLENT DATABASE MODEL , SO WHY WE HAVE TO USE SERIALIZATION !?' .
Relational database (
RD) model and serialization are very friends , RD doesn't allow multi-valued cell , and serialization handle that , you can store multidimensional array on one cell on your table as a string using serialization.


If you ignore relationships on your database model and only depend on serialized data so you are escaping from 
relational database concept,and if you ignore serialization generally  your result may be huge and complex model and that is not right.


The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.

many built-in PHP objects cannot be serialized. However, those with this ability either implement the Serializable interface or the magic __sleep() and __wakeup() methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized.

Example :
<?php// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmt$sqldata)) {
        
/* Something went wrong.. */
    
}
}
?>
 

No comments:

Post a Comment