PHP allows objects to be serialized and stored, so that, they can be accessed later. This ability is useful because if a user provides some information at the beginning (or at the start of a session), then PHP can serialize it and store it. Later, when the same user revisits, then PHP can simply retrieve the serialized data, unserialize it back as objects, and reuse them. This way, the application does not need to bother user for the same information and thus, it can enhance the ease of use.
PHP achieves serialization using the serialize() function. This function takes an object as an argument and converts it into a format where the format of the object along with its variables and values are stored. Its counterpart, unserialize() takes the serialized data as input and then returns the object with the same values as when it was serialized.
When doing unserialization, we must include the definition of the class; typically, this can be done by including a file that contains the definition of that class. For our example, we put the definition of "BasicUser" in a file "basic_serialization_user.inc"; the contents of this file look like the following:
<?php /* Include this file when serializing/unserializing BasicUser objects */ class BasicUser { var $userID, $userName, $userAge; function __construct($varID, $varName, $varAge) { $this->userID = $varID; $this->userName = $varName; $this->userAge= $varAge; } function print_userinfo() { echo "<br>Printing User Details: <br>"; echo "Userid: $this->userID<br>"; echo "Name: $this->userName<br>"; echo "Age: $this->userAge<br>"; } } ?>
Next, we provide an example that includes this file to do serialization/unserialization. This program creates two instances of the "BasicUser" class, assigns some values to their members, and then stores them in a file on the local disk. For storing serialized information ($serializeduser1 and $serializeduser2), we use file_put_contents() to write/append to a local file. However, we can also store the serialized data in a database or as a PHP session, or as PHP cookies. We chose to write to a file for the sake of simplicity.
<?php include_once('basic_serialization_user.inc'); /* Create two objects */ $user1 = new BasicUser("ksimon", "Karuna Simon", 32); $user2 = new BasicUser("jfulton", "James Fulton", 21); $user1->print_userinfo(); $user2->print_userinfo(); /* Serialize these objects */ $serializeduser1 = serialize($user1); $serializeduser2 = serialize($user2); echo "<br>",$serializeduser1; echo "<br>",$serializeduser2; /* Store the serialized data into '/tmp/serialized_object.dat' file */ file_put_contents('/tmp/serialized_object.dat', $serializeduser1); file_put_contents('/tmp/serialized_object.dat', "\n", FILE_APPEND); file_put_contents('/tmp/serialized_object.dat', $serializeduser2, FILE_APPEND); ?>
Here is the output of the above program:
Printing User Details: Userid: ksimon Name: Karuna Simon Age: 32 Printing User Details: Userid: jfulton Name: James Fulton Age: 21 O:9:"BasicUser":3:{s:6:"userID";s:6:"ksimon";s:8:"userName";s:12:"Karuna Simon";s:7:"userAge";i:32;} O:9:"BasicUser":3:{s:6:"userID";s:7:"jfulton";s:8:"userName";s:12:"James Fulton";s:7:"userAge";i:21;}
Once the serialized objects are stored in the local file, we can always open the file, read the content from the file and transform them back into the required objects. We provide a simple example (along with its output) that does the same.
<?php include_once('basic_serialization_user.inc'); $handle = fopen("/tmp/serialized_object.dat", "r"); if ($handle) { while (($buffer = fgets($handle, 4096)) !== false) { echo "<br>$buffer"; $newUser = unserialize($buffer); $newUser->print_userinfo(); } } fclose($handle); ?>
O:9:"BasicUser":3:{s:6:"userID";s:6:"ksimon";s:8:"userName";s:12:"Karuna Simon";s:7:"userAge";i:32;} Printing User Details: Userid: ksimon Name: Karuna Simon Age: 32 O:9:"BasicUser":3:{s:6:"userID";s:7:"jfulton";s:8:"userName";s:12:"James Fulton";s:7:"userAge";i:21;} Printing User Details: Userid: jfulton Name: James Fulton Age: 21