title: PHP variable visibility and behaviour with magic method
body: <p>This question is related to: <a href="https://stackoverflow.com/questions/8451116/php-magic-methods-example">PHP magic methods example</a></p>
  
  <p>I've asked this question in comment in accepted answer but I guess, it has not been noticed so I have to create this question.</p>
  
  <pre><code>&lt;?php
      class Magic {
          public $a = "A";
          protected $b = array(
              "a"=&gt;"A", 
              "b"=&gt;"B", 
              "c"=&gt;"C"
          );
          protected $c = array(1,2,3);
  
          public function __get($v) {
              echo "$v,";
              return $this-&gt;b[$v];
          }
  
          public function __set($var, $val) {
              echo "$var: $val,";
              $this-&gt;$var = $val;
          }
      }
  
      $m = new Magic();
      echo $m-&gt;a.",".$m-&gt;b.",".$m-&gt;c.",";
      $m-&gt;c = "CC";
      echo $m-&gt;a.",".$m-&gt;b.",".$m-&gt;c;
  ?&gt;
  </code></pre>
  
  <p>Output:
  <code>b,c,A,B,C,c: CC,b,c,A,B,C</code></p>
  
  <p><code>$m-&gt;c = "CC";</code> Here we already have protected variable with same name.
  So, how should this should behave in context of visibility?</p>
  
  <p>If it overwrites value of protected variable <code>c</code>, then isn't it a loop hole for protected/private variables? (I guess that would not be the case)</p>
  
  <p>If not then, the statement: <code>$this-&gt;$var = $val;</code> seems to create public variable with same name already defined as protected.
  Is that possible?</p>
  
  <p>Also, after this statement: <code>$m-&gt;c = "CC";</code>, when we access <code>$m-&gt;c</code> again, PHP calls <code>__get</code> again as if <code>c</code> has no public visibility.
  Does that mean <code>$this-&gt;$var = $val;</code> has no life time for immediate next statement? (I guess that would also not be the case)</p>
  
  <p>Can anybody please explain, it should behaves in such cases and how it gave such output?</p>