Open GuySartorelli opened 8 months ago
It’s also worth noting that we switched from Reflection to using new $class(...$values)
for a performance boost: https://github.com/silverstripe/silverstripe-framework/pull/10265
I'll add that to the caveats - that may be reason enough not to do this, but still worth discussing I reckon.
UPDATE: Apparently as of PHP 8.1 using the splat operator to pass named arguments is supported! See example 19 in https://www.php.net/manual/en/functions.arguments.php#example-505
Currently passing in named arguments to be used in use of injector (i.e.
MyClass::create('normal-arg', namedArg: 'value');
) will result in silently passing the named argument to the wrong parameter. This is done intentionally because we use the splat operator to unpack arguments to be passed to constructors, and that operator throws an error for any string keys - so we have to pass through only the values (seeInjectionCreator::create()
).We could change this to support named arguments in injection like so:
UPDATE: Apparently as of PHP 8.1 using the splat operator to pass named arguments is supported! See example 19 in https://www.php.net/manual/en/functions.arguments.php#example-505 So we don't need to use reflection at all.
Caveats
InjectionCreator
is effectively a factory (it's invoked inInjector::instantiate()
as$factory->create($class, $constructorParams)
) - this means the above solution would only work when a different specific factory has not been defined. There may therefore still be scenarios where named arguments are not respected.We swapped away from using injection for dependency injection for a performance boost - see https://github.com/silverstripe/silverstripe-framework/pull/10265