Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your assumptions are wrong.</p> <blockquote> <p>I understand that "__get called!" appears when $employee->name is accesed</p> </blockquote> <p>No, it doesn't. <code>__get</code> is only invoked when you attempt to access a member which <em>does not exist</em>. You have defined a publicly accessible member named <code>$name</code>, this is what the line <code>var $name</code> does. Reading/writing to <code>$employee-&gt;name</code> will <em>never</em> invoke the magic <code>__get</code> or <code>__set_</code> methods.</p> <p>It is not <code>$employee-&gt;name</code> that is causing <code>"__get called"</code> to be printed; <code>$employee-&gt;name</code> is returning <code>"Mario"</code>, the value you assigned to that member, and <em>that</em> is what is being printed. It is <code>$employee-&gt;age</code> that is causing <code>"__get called"</code> to be printed.</p> <p>I've tried to better explain the lines and what they do in respect to <code>__get</code>:</p> <pre><code>$employee = new Employee(); $employee-&gt;name = "Mario"; # assign to $name echo $employee-&gt;name."&lt;br /&gt;"; # read "Mario" from $name, does NOT invoke __get echo $employee-&gt;age; # attempt to read $age, invokes __get </code></pre> <p>RE: Your observation</p> <blockquote> <p>Observation : Of course __get runs when it detects $age does not exist when read, but ...when it does run, why doesn't the echo in it work?</p> </blockquote> <p>The echo <em>does</em> work. The reason you are only seeing one <code>"__get called!"</code> is because <code>__get</code> is only called once, by attempting to access <code>$employee-&gt;age</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload