I'm wondering which is semantically and technically most optimal of my choices here. I've created a simple object registry class, but the method of object access has me wondering what's best. I'm currently using the first variation:
//the Registry methods can chain, each returning a self reference
$registry = Registry::getInstance()->register('myObject', new Object);
//accessing a registered object method
//in various possible ways
Registry::getInstance()->myObject->method(); //1
Registry::getInstance()->use('myObject')->method(); //2
$registry('myObject')->method(); //3
- The first variation uses
__get()
magic, keeping with the fluent syntax. - The second uses the 'getter' method
use()
. - The third uses
__invoke()
magic, which has been suggested, but I am not too fond of.
I'm just curious to know if anyone has insight, or suggestions towards using any (or none) of these options. The reason for using a Registry class in my case is to provide pseudo-globalization of key objects, for use in nested closures (declaring them with use
every time is cumbersome)
This is somewhat related to my other question, at http://stackoverflow.com/questions/4054424/php-closures-and-implicit-global-variable-scope
Thanks in advance :)