auryn
auryn copied to clipboard
Parameter definitions not inherited from parent classes
class Dep
{
}
abstract class Foo
{
public function __construct($key, Dep $dep)
{
print_r(compact('key', 'dep'));
}
}
class Bar extends Foo
{
}
$injector = new Auryn\Injector;
$injector->define('Foo', [
':key' => 'secret',
]);
print_r($injector->make('Bar'));exit;
This currently fails with:
Auryn\InjectionException: No definition available to provision typeless parameter $key at position 0 in Foo::__construct()
The current workaround is:
$injector->defineParam('key', 'secret');
Which works globally but is not ideal.
Possibly related to #30.
This has nothing to do with scalar type hints. It's because you defined :key
for Foo
instead of Bar
.
@kelunik The problem is if you have multiple classes like Bar
that extend Foo
, and you want them to all have the same value for key
.
Yes, but it has nothing to do with scalar type declarations. It's because Auryn doesn't take the definitions for parent classes into account.
@kelunik I'll rename the PR.
Auryn intentionally does not make children from definitions of their parents. As mentioned in chat the other day this tends to come up often, so we should make a FAQ section and add it to it.
The FAQ should also provide alternatives. Sometimes it is appropriate to alias
; sometimes not. What other choices are there?
Indeed, the child constructor can be entirely different and things should not be inherited IMO. We should add something to a FAQ.
I added some words in 6c06f163800693b712ae9c9bdd601d1c912356eb to explain why it was excluded.