I’m going through some Zend PHP certification questions and am stuck on this one:
What is the output of the following:
<?php
class Magic{
public $a = "A";
protected $b = array("a" => "A", "b" => "B", "c" => "C");
protected $c = array(1,2,3);
public function __get($v){
echo "$v,";
return $this->b[$v];
}
public function __set($var, $val){
echo "$var: $val,";
$this->$var = $val;
}
}
$m = new Magic();
echo $m->a.",".$m->b.",".$m->c.",";
$m->c = "CC";
echo $m->a.",".$m->b.",".$m->c;
Answer: b,c,A,B,C,c: CC,b,c,A,B,C
I know that __get() and __set are called when trying to access/set inaccessible properties but can someone tell me what happens to the $m->a? I.e why does it disappear?
Thanks in advance
Note that the string is being concatenated, but that
__getoutputs the name of the key. The lower case letters are key names, the upper case letters are the values.ais accessed normally, not through__get,bandcare accessed through__getand the keys areecho‘d first, their value is then returned, concatenated into the string and output after the output of “b,c,”.So what this shows is that the
__getmethod is triggered forbandcbut not fora, then the values “A”, “B” and “C” are output, then the value “CC” is set, thenbandcare accessed through__getagain, then “A”, “B” and “C” are output again.