You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 (see InjectionCreator::create()).
We could change this to support named arguments in injection like so:
-// Ensure there are no string keys as they cannot be unpacked with the `...` operator-$values = array_values($params);--return new $class(...$values);+return (new \ReflectionClass($class))->newInstanceArgs($params);
We name a lot of contructor args in injection yaml configuration - but those names don't always match up with real constructor argument names. We would need to review all of these and ensure that only real constructor argument names are used.
This is what makes it a breaking change.
InjectionCreator is effectively a factory (it's invoked in Injector::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.
tl;dr: In some of those scenarios it may result in silent failures where the argument is passed to the wrong parameter.
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 ENH Faster method for creating injected instances #10265The text was updated successfully, but these errors were encountered: