rdlowrey / auryn

IoC Dependency Injector
MIT License
723 stars 65 forks source link

Parameter definitions not inherited from parent classes #133

Closed shadowhand closed 1 year ago

shadowhand commented 8 years ago
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.

kelunik commented 8 years ago

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

bvisness commented 8 years ago

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

kelunik commented 8 years ago

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.

shadowhand commented 8 years ago

@kelunik I'll rename the PR.

morrisonlevi commented 8 years ago

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 commented 8 years ago

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

kelunik commented 7 years ago

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

Danack commented 1 year ago

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

maybe direct link