gpt4 book ai didi

php - symfony: Autowiring 接口(interface)

转载 作者:可可西里 更新时间:2023-11-01 12:20:31 26 4
gpt4 key购买 nike

我需要做什么才能完成这项工作?

interface BaseServiceInterface {
public function getRecords();
}

class BaseService implements BaseServiceInterface{
public function getRecords(){
return "bla";
}
}


class SomeOtherService{

private $baseService;

public function __construct(BaseServiceInterface $baseService){
$this->baseService = $baseService;
}
}

我的 service.yml 看起来像这样:

base_service:
class: AppBundle\Service\BaseService
autowire: true

当我尝试运行它时,我得到:

Cannot autowire argument 1 for AppBundle\Service\SomeOtherService because the type-hinted class does not exist (Class BaseServiceInterface does not exist).

最佳答案

Autowiring 不直接与接口(interface)一起工作。您需要创建服务别名才能使其正常工作。

services:
AppBundle\Service\BaseServiceInterface: '@AppBundle\Service\BaseService'

引用:https://symfony.com/doc/current/service_container/autowiring.html#working-with-interfaces

关于php - symfony: Autowiring 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41395994/

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