【发布时间】:2020-09-21 15:33:54
【问题描述】:
我有一个服务叫:
<?php
class CustomService
{
private $string;
private $service1;
private $service2;
public function __construct($string, Service1 $service1, Service2 $service2)
{
$this->string = $string;
$this->service1 = $service1;
$this->service2 = $service2;
}
//Custom functions
}
$string 参数是一个可以改变其值的字符串。
我想从其他服务调用此服务,但有没有办法在我的 services.yaml 中使用某种别名定义“CustomService”?
service:
App\Service\CustomService:
alias: preService1
arguments:
$string: 'ABCD'
App\Service\CustomService:
alias: preService2
arguments:
$string: 'EFGH'
并在另一个服务中注入由别名定义的服务之一:
<?php
class PurchaseService
{
public function __construct(CustomService $preService1)
{
//Here $preService1->string must be 'ABCD'
}
//Or in other functions in this service $preService1->string must be 'ABCD'
}
【问题讨论】: