代码拉取完成,页面将自动刷新
一个简易的服务容器模式(LoC)的实现,本包将实现以下功能:
$app = Bg\Container\Container::getInstance();
$app->make(\App\Example::class);
假如现在有一个类 \App\Example ,其依赖于类 \App\Inject ,代码如下:
namespace App;
class Example
{
private $obj;
public function __construct(Inject $obj)
{
$this->obj = $obj;
}
public function getValue()
{
return $this->obj->getValue();
}
}
class Inject
{
public function getValue()
{
return 'inject';
}
}
类 \App\Example 的构造函数需要 \App\Inject 的实例参数,使用服务容器,我们不需要手工对 \App\Inject 实例化,服务容器将替我们完成这项工作:
$example = $app->make(\App\Example::class);
echo($example->getValue());
以上例子将输出: ==inject==
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。