On Mon, Feb 18, 2013 at 8:41 AM, Jeroen De Dauw <jeroendedauw(a)gmail.com>wrote;wrote:
A lot of components in core, for instance
SpecialPages, API modules and
Actions allow registering new components as follows:
$someList['some-name'] = 'YourHandlingClass';
The problem with this is that the extending code has no control over the
instantiation of the handling object. So you can't inject any dependencies.
That is more then a little bad design wise.
This all ties in with the class autoloader and the decision years ago to do
lazy-loading for code rather than preloading all classes and functions. By
specifying a class name rather than a live object, we let the actual code
for the class sit unloaded until something actually uses it.
So if that's something we want to maintain -- and it helps performance *a
lot* for small installs when there's not an opcode cache in use -- it's
worth thinking about ways to inject your data without having to instantiate
much live code.
One way would to specify a callback. For PHP 5.3 and later we can use
closures...
$blah = $something;
$someList['some-name'] = function() use ($blah) {
return new YourHandlingClass($blah);
};
and then have the instantiating code check if it's been given a callable
function rather than a class name. Since this doesn't run the function
until it's used, the autoloader still doesn't load the class until it's
actually run.
-- brion