我写这样的课:
class SERVICE
{
public function __construct($a, UserRepository $repository) {
$this->repository = $repository; $this->relations = [
[
'\Api\Users\Models\Client', 'clients', '$a'
]
];
$this->events = [ ];
}
...
}
我使用了这样的类:
use SERVICE;
class TEST
{
public function __construct(SERVICE $service)
{
$this->service = $service;
}
}
我有错误:
无法解析的依赖项解析[Parameter#0 [$ a]]
如何以这种方式感应参数?
答案 0 :(得分:1)
Laravel只能自动注入https://laravel.com/docs/5.5/container#automatic-injection。对于您来说,服务中的$a
参数未定义类型。您必须是更改或服务__construct或Test
类。例如
use SERVICE;
class TEST
{
public function __construct(UserRepository $repository) {
$this->service = new Service('some-value', $repository);
}
}
或更改服务
class SERVICE
{
public function __construct(UserRepository $repository) {
$this->repository = $repository;
$this->events = [ ];
}
public function setRelations($a)
{
$this->relations = [
[
'\Api\Users\Models\Client', 'clients', '$a'
]
];
}
...
}
和用法
use SERVICE;
class TEST
{
public function __construct(SERVICE $service)
{
$this->service = $service;
$this->service->setRelations('some-value');
}
}