PHP  Object serialize

in php object serialization allows an object to be transformed into a sequence of bytes that can later be re-created (deserialized) into the original object.

the object serialization and deserialization mechanism is generally known as persistence.

in php this process done by serialize() method.

the serialize() returns a string containing a byte-stream representation of any value that can be stored in php.


         serialize( value )

value is the valid any type supported by php.

  class      mouse{
        public  static   $mouse_company  =  "microsoft";
        public  $mouse_color   = "black";
        public  $mouse_price   = "$44";
        public  function    mouse_property_dis(){
                echo    "mouse company = ".mouse::mouse_company;
                echo    "<p> mouse color = ".$this->mouse_color."</p>";
                echo    "<p> mouse price = ".$this->mouse_price."</p>";
        $mouse_obj  =   new  mouse();
        $seria =   serialize( $mouse_obj );//this statement serialized the mouse object
        echo  " this is serialized mouse object = ". $seria;

PHP Object unserialize

php unserialize() method takes a string created by serialize() and turns it back into a usable object.

you can use serialize and unserialize statement on any php value.

  class      keyboard{
             public  static   $keyboard_company  =  "audi";
             public  $keyboard_color   = "green";
             public  $keyboard_price   = "$44444";
             public  $keyboard_array  =  array( "one"=>"$222","two"=>"$333",
                                               "three"=>"$444","four"=>"$555" );
             public  function    keyboard_value_show(){
                          echo    "<p> keyboard color = ".$this->keyboard_color."</p>";
                          echo    "<p> keyboard price = ".$this->keyboard_price."</p>";}
     $keyboard_obj =   new  keyboard();
     $seria =   serialize( $keyboard_obj );//this statement serialized the keyboard object
     echo  " this is serialized keyboard object = ". $seria;
     $unseria =unserialize($seria);//this statement unserialize the serialize object
     echo  "<br/> array[one] = ".$unseria->keyboard_array['one'];
     echo  "<br/> array[two] = ".$unseria->keyboard_array['two'];  
     echo  "<br/> array[three] = ".$unseria->keyboard_array['three'];
     echo  "<br/> array[four ] = ".$unseria->keyboard_array['four'];

PHP  object cloning

the object cloning is a way to create exact copy of an object.

in the php language an object copy is created by using the clone keyword.


         $copy   =    clone  $object

note : when an object is cloned, a shallow copy of all of the object's properties.any properties that are references to other variables, will remain references.

           class   price{
                                 public   $value;
                function    _construct(){
                                         $this->value =  $this->value;
           $p_obj =  new  price();
           $p_obj->value = "dollar";
           $clone_price =  clone  $p_obj; // careate shallow copy of an price object
           echo  "price object value = ".$p_obj->value;
           echo  "<br/> clone_price object value = ".$clone_price->value;

php object iteration

the php foreach loop can be used to iterate an object.

by default, all visible properties will be used for the iteration.

            class    audi{
                   protected     $company  = "audi" ;
                   private       $product_id = "34fe34";
                   public    $color  =  "red";
                   public    $price  =  "$34334";
  public  function   iterate(){
  foreach( $this  as  $key => $value ){
        echo  "inside the  audi  class  object   key = $key ...... value = $value";
        echo  "<br/>"; 
   } }
   $audi  =  new  audi();
   foreach( $audi  as  $key => $value ){
     echo  "outside the audi   class  object   key = $key ...... value = $value";
     echo  "<br/>";

           /* foreach loop  easily iterate object as a key and value 
                $key represent the object  properties
                $value represent the object properties value 
                outside the class private properties cannot be accessible  */                                             
            echo "<br/><br/>";
            $audi->iterate();  // call the iterate method