title: PHP set object properties dynamically
body: <p>I have a function, that should read array and dynamically set object properties. </p>
  
  <pre><code>class A {
      public $a;
      public $b;
  
      function set($array){
          foreach ($array as $key =&gt; $value){
              if ( property_exists ( $this , $key ) ){
                  $this-&gt;{$key} = $value;
              }
          }
      }
  }
  
  $a = new A();
  $val = Array( "a" =&gt; "this should be set to property", "b" =&gt; "and this also");
  $a-&gt;set($val);
  </code></pre>
  
  <p>Well, obviously it doesn't work, is there a way to do this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>It seems that nothing is wrong with this code, the question should be closed</p>