title: What is the real purpose of magic method __set_state in PHP?
body: <p>I'm learning about magic methods, reading every site, taking every example, but nothing makes sense to me. Examples like this:</p>
  
  <pre><code>class A
  {
      public $var1;
      public $var2;
  
      public static function __set_state($an_array) // As of PHP 5.1.0
      {
          $obj = new A;
          $obj-&gt;var1 = $an_array['var1'];
          $obj-&gt;var2 = $an_array['var2'];
          return $obj;
      }
  }
  
  $a = new A;
  $a-&gt;var1 = 5;
  $a-&gt;var2 = 'foo';
  
  eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array(
                                              //    'var1' =&gt; 5,
                                              //    'var2' =&gt; 'foo',
                                              // ));
  var_dump($b);
  </code></pre>
  
  <p>What is the real purpose? Is for debugging or something?</p>