This is simple example to understand serialize and unserialize a object in php.
we covert object into string using serialization and use this object current status (with assign values) after unserialization on other page..
c.php
<?php class A {
public $one ;
public function A($val) {
$this->one=$val;
// echo $this->one;
}
function display(){
echo $this->one;
}
}
?>
c.php a file have class with name A.
a.php
<?
require_once "c.php";
$ob= new A('by Pankaj Raghuwanshi : Object Searlization.');
$ob->display(); // Output is: by Pankaj Raghuwanshi : Object Searlization.
$s = serialize($ob);
// echo $s will show a string of an object
?>
<br><A href='b.php?s=<?=$s;?>'>B-file</a>
We serialize this object convert into string and pass this string into another page by get method.
Note : we can pass this string one page to another page with various method like using session, we can save into DB and fetch another page, save into text file.
We will unserialize this object on another file name is b.php
b.php
<?
require_once "c.php";
$ob = unserialize($_GET[s]);
$ob->display();
// Output is: by Pankaj Raghuwanshi : Object Searlization.
?>
after unserialization, object showing same behavior like a.php file
and assign value of a.php still is in memory of object . if we will unserialize this object after many http request . Object will persist all assign values in their memory.