标签
偶尔你可能需要解析绑定中的某个「类」。例如你正在建设一个汇总报表,它需要接收实现了 Report 接口的不同实现的数组。在注册了 Report 的这些实现之后,你可以用 tag 方法来给他们赋予一个标签:
$this->app->bind("SpeedReport", function()
{
//
});
$this->app->bind("MemoryReport", function()
{
//
});
$this->app->tag(["SpeedReport", "MemoryReport"], "reports");
一旦服务打上标签,可以通过 tagged 方法轻易地解析它们:
$this->app->bind("ReportAggregator", function($app)
{
return new ReportAggregator($app->tagged("reports"));
});