Today we will be learning a very important topic in OOP’s called serialization which is used between webpages and applications, in this Serialization in PHP tutorial.
- Serialization is used to convert an object to a byte-stream representation and vice-versa.
- This is useful when we need to pass object data in the form of string of text between scripts and applications.
- There are various situations in which we need to pass objects in the form of string such as:
- Converting object to string and storing it in a text file or a database table.
- Converting and passing objects in URL query string.
- Carrying objects between webpages in the session, etc.
- To convert a object to a string and then back to object when required we can use following functions:
- serialize(): the serialize() function takes the object and converts it to a string representation containing class name and its properties.
- unserialize(): the unserialize() function takes the serialized string or string that is obtained from object and converts it again to the object.
- When the object is serialized, the content is placed with some type of a specifier followed by a colon, then followed with the actual data followed by a semi-colon.
- When we serialize an object it stores the class name and all its properties. It doesn’t store methods of the class. Hence to unserialize the serialized object the class should be present in the script before unserializing it.
- So we have to unserialize the object in the script where the class is defined or we can include the class in the script where we want to unserialize the object with the include statement as shown below:
<?php include(“worker.inc”); //some code here unserialize($work); ?>
<?php class Worker { private $id; protected $name; protected $dept; public function __construct($id,$name,$dept) { $this->id=$id; $this->name=$name; $this->dept=$dept; } public function display() { echo "ID : ".$this->id."<br>Name : ".$this->name."<br> Department : ".$this->dept; } } //creating an instance of Worker class $work=new Worker(1,'James Das','Development'); //serializing the object $work using serialize() function $serialized=serialize($work); //displaying the serialized string echo "*Serialized object in string format:<br>".$serialized."<br><br>"; //unserializing the string $serialized to the object back $work1=unserialize($serialized); //calling the function display() of the Worker class using object $work1 echo "*Object Unserialised:<br>"; $work1->display(); ?>
$serialized=serialize($work);
$work1=unserialize($serialized);
<?php class Worker { private $id; protected $name; protected $dept; public function __construct($id,$name,$dept) { $this->id=$id; $this->name=$name; $this->dept=$dept; } public function display() { echo "ID : ".$this->id."<br>Name : ".$this->name."<br> Department : ".$this->dept; } public function __sleep() { echo "*Cleaning the Worker object.<br>"; return array("name","dept"); //return array_keys(get_object_vars($this)); } public function __wakeup() { echo "*Setting the object.<br>"; } } //creating an instance of Worker class $work=new Worker(1,'James Das','Development'); //serializing the object $work using serialize() function $serialized=serialize($work); //displaying the serialized string echo "*Serialized object in string format:<br>".$serialized."<br><br>"; //unserializing the string $serialized to the object back $work1=unserialize($serialized); //calling the function display() of the Worker class using object $work1 echo "*Object Unserialised:<br>"; $work1->display(); ?>
Thus we can convert an object to byte stream and again retrieve the string to an object using serialization and unserialization respectively, which is very useful to pass data between scripts, and applications. Serialization is mostly used in sessions.
So today we learned a new and interesting term in this Serialization in PHP tutorial.