gpt4 book ai didi

typo3 - 如何在构造函数中使用注入(inject)的对象?

转载 作者:行者123 更新时间:2023-12-01 09:02:57 24 4
gpt4 key购买 nike

我的 Extbase 扩展中有一个服务类,我想使用 ObjectManager 在构造函数中创建一个对象的实例。

/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
* @inject
*/
protected $objectManager;

public function __construct() {
$this->standaloneView = $this->objectManager->get('TYPO3\CMS\Fluid\View\StandaloneView');
$this->standaloneView->setFormat('html');
}

不幸的是,这并没有因为错误 Call to a member function get() on null 而失败,因为注入(inject)的类似乎在构造函数中不可用。如何在构造函数中使用注入(inject)类?

最佳答案

为此,我可以使用所谓的构造函数注入(inject)。 ObjectManagerInterface 被定义为构造函数的参数,然后由 Extbase 自动注入(inject):

/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
*/
protected $objectManager;

public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
$this->objectManager = $objectManager;
$this->standaloneView = $this->objectManager->get('TYPO3\CMS\Fluid\View\StandaloneView');
$this->standaloneView->setFormat('html');
}

关于typo3 - 如何在构造函数中使用注入(inject)的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29915782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com