title: Opposite of __set_state() like __get_state()?
body: <p>Is there an opposite PHP function for <code>__set_state()</code> like <code>__get_state()</code>? And I don't mean <code>__sleep()</code> for serialization. I want a simple function which is called after <code>var_export()</code> is called on a object but before <code>var_export()</code> gets the data so I can choose on each object which data will be exported. <del>I know there is a way to implement this with <code>__get()</code> and <code>debug_backtrace()</code> to modify the data only when <code>var_export()</code> is called on a object.</del> But is there a simpler way?</p>
  
  <p><strong>Edit</strong>: There is no way to implement this with <code>__get()</code> and <code>debug_backtrace()</code> to modify the data only when <code>var_export()</code> is called on a object because <code>__get()</code> is not called on <code>var_export()</code>.</p>
  
  <p><strong>Solution</strong>:</p>
  
  <pre><code>&lt;?php
  /*
   * @author Christian Mayer &lt;http://fox21.at&gt;
   * @link http://stackoverflow.com/q/21762276/823644
   * @link https://eval.in/163041
   * @link https://eval.in/163462
   * @link https://eval.in/163909
   * @link https://gist.github.com/TheFox/49ff6903da287c30e72f
   */
  
  interface Exportable{
      public function __get_state();
  }
  
  function unset_with_get_state($expression){
      $before = clone $expression;
      $classVars = array_keys(get_class_vars(get_class($before)));
      foreach(array_diff($classVars, $before-&gt;__get_state()) as $var){
          unset($before-&gt;$var);
      }
      return $before;
  }
  
  function my_var_export($expression, $return = null){
      $before = $expression;
      if($before instanceof Exportable){
          $before = unset_with_get_state($expression);
      }
      return var_export($before, $return);
  }
  
  class Foo implements Exportable{
      public $name = null;
      public $foo = null;
      public $bar = null;
  
      public function __get_state(){
          // Only show 'name' and 'bar' on my_var_export().
          return array('name', 'bar');
      }
  }
  
  $a = 'hello';
  my_var_export($a);
  print "\n";
  
  $b = new Foo();
  $b-&gt;name = 'world';
  $b-&gt;foo = 'foo is foo';
  $b-&gt;bar = 'bar is bar';
  my_var_export($b);
  print "\n";
  </code></pre>
  
  <p>Of course with an own implementation you can do everything. With <code>is there a simpler way?</code> I mean if there is a built-in PHP function or something like that so you don't have to do it yourself. This solution is not really <em>easy</em> because you must extend all your objects from <code>Exportable</code>. And this also only works when your variables are <code>public</code>. In this example I choosed to export only <code>name</code> and <code>bar</code> but not <code>foo</code>. A built-in PHP function (like <code>__set_state()</code> is) would be more nicer.</p>