当我们需要访问存储库中的另一个类时,下面的技术就像预期的那样工作。
即。类AbcRepo实现AbcInterface {
}
namespace App;
class DefRepo implements DefInterface {
protected $abc;
public function __construct(AbcInterface $abc) {
$this->abc = $abc;
}
} 因此,当我在服务提供商中注册时:
$app->bind('App\DefInterface', function($app) {
return new App\DefRepo(App::make('App\AbcInterface'));
});困扰我的问题是:
new App\DefRepo(App::make('App\AbcInterface')); 这是正确的方法吗。我们(我的意思是指我)从不对提供的服务进行单元测试,因此我可以简单地忽略它。但如有任何意见,我们将不胜感激。
发布于 2015-01-05 08:30:04
这是正确的,但是,最好让键入的依赖项注入初始化RefRepo。
$app->bind('App\DefInterface', 'App\DefRepo');
$app->bind('App\AbcInterface', 'App\AbcRepo');这意味着,当需要实现DefRepo的类时,Laravel将尝试初始化DefInterface。因为DefRepo需要AbcInterface,所以类AbcRepo将被注入实例中。
https://stackoverflow.com/questions/27774813
复制相似问题