TYPO3 Symfony 依赖注入构造函数,带有运行时参数和类

问题描述 投票:0回答:1

是否可以定义一个同时处理运行时参数和依赖注入的构造函数?如果我这样做:

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”是类型提示的“数组”,您应该显式配置其值。

symfony dependency-injection typo3
1个回答
0
投票

我觉得你的课应该是这样的:

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']);
© www.soinside.com 2019 - 2024. All rights reserved.