class Router { public function init() { echo "-INIT"; } } class Application { //По идее должно отработать при ->router public function __get($name) { echo "-GET"; } public function __construct() { $this->router = new Router(); //свойство с ссылкой Framework::$app = $this; //В хелпер линк на себя } } class Framework { public static $app; } new Application(); //Инициализация Framework::$app //Instance of Application ->router //Но почему тут не срабатывает __get??? ->init(); |
1 answer
And it should not work if I correctly understood your code. You defined the router property in the code:
$this->router = new Router(); and __get is called only for "unavailable" properties:
Overload methods are invoked when interacting with properties or methods that were not declared or not visible in the current scope.
Read more: http://php.net/manual/ru/language.oop5.overloading.php
- I suppose that's not the point. If, after assigning $ this-> router, it accesses the property in the constructor, then __get will work. But I think that this is somehow related to the scope - Ninazu
- one@Ninazu to start with - why do you even admit that this is not the case? Written clearly - for indefinite properties. And you have determined. You can find out at 100%: remove the line
$this->router = new Router();and try to run. If I'm right, __get will be called. If not (I don’t deny that I can miss something), then the situation is more interesting and worth considering. - Ivan Pshenitsyn - one@Ninazu If I'm not mistaken, when the scope is not specified (as in your case,
$this->router = new Router();) the property is assigned to public. Those. it will be available for external access and there are no reasons for calling __get. - Ivan Pshenitsyn - I just wanted to do a deferred initialization. In any case, the already rewritten logic now works as desired, thanks for responding. - Ninazu
|