I don't seem to understand why the code below prints "TEST" only twice.
<?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; } } $a = new A(); $a->test; $a2 = A::getInstance(); $a2->test; A::someStaticMethod(); ?>
The PHP site says ( link ):
Property overloading only works in the context of an object. These magic methods will not be triggered in a static context. Therefore, these methods should not be declared static. Starting with PHP 5.3.0, a warning is issued if one of the overload magic methods is declared static.
But I think they are trying to say that u should declare magic methods static. eg:.
public static function __get () {}
Plus the fact that I actually use it in the context of an object. $ a = new self (); returns an instance of class A in the variable $ a. Then I use $ a-> test (object context imo?) To retrieve the private variable "test", which in turn must be overloaded ...
I'm confused...
source share