Skip to content

Extending other object definitions (e.g. parent classes) #279

@alwynn

Description

@alwynn

Hi. Is there a way in PHP-DI to define dependencies for an abstract object? Most logical way, to define parameters for a constructor for abstract object which would later be resolved for child object, does not work:

abstract class AbstractObject {
   protected $a;
   public function __construct($a) { $this->a = $a; }
}
class ConcreteObject extends AbstractObject {}

$builder = new DI\ContainerBuilder;
$builder->useAutowiring(true);
$builder->addDefinitions([
   'AbstractObject' => DI\object()->constructorParameter('a', 'abc')
]);
$container = $builder->build();
$container->make('ConcreteObject');

This code result in

PHP Fatal error:  Uncaught exception 'DI\Definition\Exception\DefinitionException'
with message 'Entry ConcreteObject cannot be resolved: The parameter 'a' of 
AbstractObject::__construct has no value defined or guessable
Full definition:
Object (
    class = ConcreteObject
    scope = singleton
    lazy = false
    __construct(
        $a = #UNDEFINED#
    )
)'

Am I missing something from the documentation or PHP-DI is unable at this point to realize this scenario?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions