![](/img/trans.png)
[英]Laravel service container is it possible to bind to class AND all it's ancestors - like in Symfony
[英]how to bind two class in Laravel Service Container
我有一個名為Cat.php
的類,如下所示:
class Cat {
public $animal;
public function __construct(MyInterface $animal)
{
$this->animal = $animal;
}
}
我有一個名為其他類Animal.php
並實現MyInterface
我需要使用Cat
類和Animal
類入口
它對我有用:
app()->bind('Cat', function (){
return new Cat(new Animal());
});
但我不喜歡它。 所以我測試了以下代碼:
app()->bind('Cat', Cat::class);
並使用類如下:
$cat = app()->make('Cat');
但我的問題是這段代碼找不到 Animal 類。 我如何將動物課引入貓課?
你能不能試試,
app()->makeWith('Cat', new Animal())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.