Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions container.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Binding Typed Variadics](#binding-typed-variadics)
- [Tagging](#tagging)
- [Extending Bindings](#extending-bindings)
- [Binding Tags](#binding-tags)
- [Resolving](#resolving)
- [The Make Method](#the-make-method)
- [Automatic Injection](#automatic-injection)
Expand Down Expand Up @@ -221,6 +222,46 @@ Once the services have been tagged, you may easily resolve them all via the `tag
return new ReportAggregator($app->tagged('reports'));
});


<a name="binding-tags"></a>
### Binding Tags

If a class needs an array of instances or a variadic constructor argument, you can instruct the container to resolve that dependency with tagged services.

To bind services tagged as "reports" to a primitive (`array`) constructor argument named `$reports`:

class ReportAggregator
{
/** @var Report[] */
public $reports;

public function __construct(array $reports)
{
$this->reports = $reports;
}
}

$this->app->when(ReportAggregator::class)
->needs('$reports')
->giveTagged('reports');

To bind services tagged as "reports" to a variadic constructor argument typed `Report`:

class ReportAggregator
{
/** @var Report[] */
public $reports;

public function __construct(Report ...$reports)
{
$this->reports = $reports;
}
}

$this->app->when(ReportAggregator::class)
->needs(Report::class)
->giveTagged('reports');

<a name="extending-bindings"></a>
### Extending Bindings

Expand Down