Skip to content

Autowiring and Annotations do not work for DI\object() inside arrays #343

@mopahle

Description

@mopahle

Example code:

<?php
require 'vendor/autoload.php';

class A {
  public function __construct(B $b) {}
}

class B {
  /**
   * @Inject
   */
  public function test() {
    echo "Autowiring and Annotations work.";
  }
}

$builder=new DI\ContainerBuilder();
$builder->useAnnotations(true);
$builder->addDefinitions([
  'test'=>[DI\object(A::class)]
]);
$container=$builder->build();

try {
  $container->get('test');
}
catch (Exception $e)
{
  echo $e->getMessage();
}

Executing the code above gives the following output:

Error while resolving test[0]. Entry  cannot be resolved: The parameter 'b' of A::__construct has no value defined or guessable
Full definition:
Object (
    class = A
    scope = singleton
    lazy = false
)

changing the definitions to $builder->addDefinitions(['test'=>DI\object(A::class)]); works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions