auryn icon indicating copy to clipboard operation
auryn copied to clipboard

Parameter definitions not inherited from parent classes

Open shadowhand opened this issue 9 years ago • 7 comments

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.

shadowhand avatar Dec 22 '15 19:12 shadowhand

This has nothing to do with scalar type hints. It's because you defined :key for Foo instead of Bar.

kelunik avatar Dec 22 '15 19:12 kelunik

@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.

bvisness avatar Dec 22 '15 19:12 bvisness

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 avatar Dec 22 '15 19:12 kelunik

@kelunik I'll rename the PR.

shadowhand avatar Dec 22 '15 19:12 shadowhand

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.

morrisonlevi avatar Dec 23 '15 01:12 morrisonlevi

The FAQ should also provide alternatives. Sometimes it is appropriate to alias; sometimes not. What other choices are there?

morrisonlevi avatar Dec 23 '15 01:12 morrisonlevi

Indeed, the child constructor can be entirely different and things should not be inherited IMO. We should add something to a FAQ.

kelunik avatar Jan 09 '17 11:01 kelunik

I added some words in 6c06f163800693b712ae9c9bdd601d1c912356eb to explain why it was excluded.

maybe direct link

Danack avatar Jan 11 '23 15:01 Danack