標(biāo)簽

2018-02-24 15:51 更新

偶爾你可能需要解析綁定中的某個(gè)「類」。例如你正在建設(shè)一個(gè)匯總報(bào)表,它需要接收實(shí)現(xiàn)了 Report 接口的不同實(shí)現(xiàn)的數(shù)組。在注冊(cè)了 Report 的這些實(shí)現(xiàn)之后,你可以用 tag 方法來給他們賦予一個(gè)標(biāo)簽:

$this->app->bind('SpeedReport', function()
{
    //
});

$this->app->bind('MemoryReport', function()
{
    //
});

$this->app->tag(['SpeedReport', 'MemoryReport'], 'reports');

一旦服務(wù)打上標(biāo)簽,可以通過 tagged 方法輕易地解析它們:

$this->app->bind('ReportAggregator', function($app)
{
    return new ReportAggregator($app->tagged('reports'));
});
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)