i dont seem to understand why the code below only prints "TEST" two times.
<?php
class A {
private $test = "TEST<br />";
public static function getInstance() {
return new self();
}
public static function someStaticMethod() {
$a = new self();
$a->test;
}
public function __get($args) {
echo $this->$args;
}
}
/* echo's "TEST" */
$a = new A();
$a->test;
/* echo's "TEST" */
$a2 = A::getInstance();
$a2->test;
/*
No output... eeerhm... how come?
Why is $a->test (inside someStaticMethod()) not being overloaded by __get ??
*/
A::someStaticMethod();
?>
PHP site says (link):
Property overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods should not be declared static. As of PHP 5.3.0, a warning is issued if one of the magic overloading methods is declared static.
But i think they are trying to say u should declare the magic methods static. e.g.:
public static function __get(){}
Plus the fact the i am in fact using it in object context. $a = new self(); returns an instance from class A in variable $a. Then i am using $a->test (object context imo?) to fetch the private "test" variable which in turn should get overloaded...
i am confused...