【发布时间】:2020-01-21 13:31:16
【问题描述】:
我有一个简单的类,如下所示:
<?php
namespace App\Algorithm;
use App\Dao\MatchDao;
use App\Service\MatchService;
class Calculator {
private $users;
private $matchDao;
function __construct(MatchService $matchService, MatchDao $matchDao) {
$this->users = $matchService->users;
$this->matchDao = $matchDao;
}
public function hourlyRate() {
$query = $this->matchDao->getSingleColumn('Payment', 'hourly_rate', 32);
var_dump($query);
}
}
但我收到以下错误消息:
检测到服务“App\Algorithm\Calculator”的循环引用, 路径:“App\Algorithm\Calculator -> App\Service\MatchService -> 应用\算法\计算器”。
MatchService.php
<?php
namespace App\Service;
use App\Algorithm\Calculator;
use App\Algorithm\Collection;
class MatchService {
public $users;
private $collection;
private $calculator;
function __construct(Collection $collection, Calculator $calculator) {
$this->collection = $collection;
$this->calculator = $calculator;
}
public function getMatch($data) {
$this->users = $this->collection->getAllUsers($data);
$this->calculator->hourlyRate();
return 1;
}
}
问题是MatchService,但我到底做错了什么?
【问题讨论】:
-
你能给出 MatchService 的定义吗?
-
你在构造函数中被依赖注入的东西也依赖注入计算器
-
@aynber 当然,我已经更新了我的问题。
-
@MylesK 这是真的。所以这意味着依赖注入不起作用,我必须创建一个新实例?
-
Calculator需要MatchService,反之亦然