7

I was reading about serialize/unserialize concepts of PHP. I was wondering how they are stored in the filesystem/db. I guess it is in the binary format. However, I wonder how entire class is stored? I understood that data in the data member can be stored but how are the methods stored?

I mean, how does PHP know what code is written inside the function of say, someFunc()?

$obj = new ClassName();
$obj->someFunc();
$serial = serialize($obj);
$unserialobj = unserialize($serial);
$unserialobj->someFunc();

PHP can know what to do at line #2, but how it know what to do at line #5 which is an unserialized object? Does it save the code as well?

Bas Peeters
  • 3,269
  • 4
  • 33
  • 49
dejjub-AIS
  • 1,501
  • 2
  • 24
  • 50
  • 3
    `serialize()` does not store anything anywhere. It just serializes an object and gives you the result as a string. – BoltClock Oct 24 '11 at 09:12

3 Answers3

11

When serializing an object, PHP only stores the object's current state, i.e. its property values. It does not serialize its methods. The corresponding class needs to be loaded in memory at the time of unserialization. PHP will restore the state of the object from the serialized string and take the rest of the information (structure and methods) from the class of the same name.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • can you please put some lite on what yo umean by "the corresponding clss needs to be loaded in memory at the time of unserialization"? how do i do that? – dejjub-AIS Oct 24 '11 at 09:40
  • 2
    @user Just `include 'my_class.php';`. PHP only needs to have seen the definition of your class. In other words, if you can do `new ClassName`, then you can unserialize an object of type `ClassName`. – deceze Oct 24 '11 at 09:54
6

PHP can know what to do at line#2 but how it knows what to do at line#5 which is a unserialized object? does it save the code as well?

Yes, serialize() will save the information about the class which this object is an instance of, along with its state, so when you unserialize, you get an instance of that class, which in this case is ClassName.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

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.

PankajR
  • 340
  • 3
  • 12