所以我有一个BaseValuation
抽象类,它是实现示例FooValuation
和BarValuation
.
我想根据使用输入实例化Foo
或实现。Bar
所以我自己创建了一个简单的类Valuation
,它只是这样做:
<?php
namespace App\Valuation;
class Valuation
{
public $class;
public function __construct($id, $type = 'basic')
{
$class = 'App\Valuation\Models\\' . ucfirst($type) . 'Valuation';
$this->class = new $class($id);
}
public function make()
{
return $this->class;
}
}
使用它我可以简单地做(new App\Valuation\Valuation($id, $type))->make()
,我会根据用途要求得到所需的实现。
但我知道 laravel 的容器很强大,必须让我以某种方式做到这一点,但我不明白如何做到这一点。有什么想法吗?