title: PHP - Indirect modification of overloaded property
body: <p>I know this question has been asked several times, but none of them have a real answer for a workaround. Maybe there's one for my specific case.</p>
  
  <p>I'm building a mapper class which uses the magic method <code>__get()</code> to lazy load other objects. It looks something like this:</p>
  
  <pre><code>public function __get ( $index )
  {
      if ( isset ($this-&gt;vars[$index]) )
      {
          return $this-&gt;vars[$index];
      }
  
      // $index = 'role';
      $obj = $this-&gt;createNewObject ( $index );
  
      return $obj;
  }
  </code></pre>
  
  <p>In my code I do:</p>
  
  <pre><code>$user = createObject('user');
  $user-&gt;role-&gt;rolename;
  </code></pre>
  
  <p>This works so far. The <code>User</code> object doesn't have a property called 'role', so it uses the magic <code>__get()</code> method to create that object and it returns its property from the 'role' object.</p>
  
  <p>But when i try to modify the 'rolename':</p>
  
  <pre><code>$user = createUser();
  $user-&gt;role-&gt;rolename = 'Test';
  </code></pre>
  
  <p>Then it gives me the following error:</p>
  
  <blockquote>
    <p><strong>Notice: Indirect modification of overloaded property has no effect</strong></p>
  </blockquote>
  
  <p>Not sure if this is still some bug in PHP or if it's "expected behaviour", but in any case it doesn't work the way I want. This is really a show stopper for me... Because how on earth am I able to change the properties of the lazy loaded objects??</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>The actual problem only seems to occur when I return an array which contains multiple objects.</p>
  
  <p>I've added an example piece of code which reproduces the problem:</p>
  
  <p><a href="http://codepad.org/T1iPZm9t" rel="noreferrer">http://codepad.org/T1iPZm9t</a></p>
  
  <p>You should really run this in your PHP environment the really see the 'error'. But there is something really interesting going on here.</p>
  
  <p>I try to change the property of an object, which gives me the notice 'cant change overloaded property'. But if I echo the property after that I see that it actually DID change the value... Really weird...</p>