在使用PHP-DI向构造函数注入已定义的参数时遇到麻烦


Trouble injecting defined parameters into constructor using PHP-DI

这可能看起来很简陋,但我似乎无法在不使用注释的情况下直接将任何参数注入到类构造函数中。下面是定义,类名为

    $shell->set('root','[Root Definition Here]');
    $shell->make('Namespace'To'Product');
    Class Product{
          public function __construct($root){
               //coding continues here
          }
    }

但是我一直得到这个错误

未捕获异常' exception ',消息'Entry '"Namespace'To'Product"无法解析:__construct()的参数$root没有定义值或没有可猜值

然而,如果我使用注释,这个问题将得到解决。但是我真的不想在每次注入参数时都使用注释。

这里有什么问题?

谢谢

PHP-DI使用类型提示而不是参数名进行注入。因此,如果$root有一个类型提示(例如Foo'Bar $root),它将工作,但现在它不能工作。

你必须手动定义参数:

$container->set(
    'Namespace'To'Product',
    DI'object()->constructor(DI'get('root')
);