Private variables are not hidden in the class. So, when executing the code below, you can get the $ b variable without calling __get ():
class A { private $b; function get_b() { return $this->b; } } $a = new A; echo $a->get_b();
But below, you cannot - it will call the __get () function:
class A { private $b; function get_b() { return $this->b; } } $a = new A; echo $a->b;
So in your example:
class A {private $ b;
function __construct() { unset($this->b); } function __get($n) { echo '!broken!'; return $this->$n; } }
$a = new A;
In __get (), return $ this → $ n "know the variable $ n - that is, $ b (as in my first code example, the get_b () function). So this is not infinite recursion, it is like any other function that calls ;)
source share