是否可以定义一个同时处理运行时参数和依赖注入的构造函数?如果我这样做:
class MyClass {
protected array $runtimeArg;
public function __construct(
array $runtimeArg,
private readonly NewsRepository $newsRepository
)
{
$this->runtimeArg = $arg;
}
}
$myClass = GeneralUtility::makeInstance(MyClass::CLASS, 'foo');
我得到: 无法自动装配服务“..MyClass”:方法“__construct()”的参数“$runtimeArg”是类型提示的“数组”,您应该显式配置其值。
我觉得你的课应该是这样的:
class MyClass {
protected array $runtimeArg;
public function __construct(
array $runtimeArg,
private readonly NewsRepository $newsRepository
)
{
$this->runtimeArg = $runtimeArg; // Fix the typo here
}
}
// using array while creating instance
$myClass = GeneralUtility::makeInstance(MyClass::CLASS, ['foo']);