Skip to content

Commit 5b49ce9

Browse files
committed
Document binding tags (giveTags)
1 parent 6a5add4 commit 5b49ce9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

container.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Binding Typed Variadics](#binding-typed-variadics)
1010
- [Tagging](#tagging)
1111
- [Extending Bindings](#extending-bindings)
12+
- [Binding Tags](#binding-tags)
1213
- [Resolving](#resolving)
1314
- [The Make Method](#the-make-method)
1415
- [Automatic Injection](#automatic-injection)
@@ -221,6 +222,46 @@ Once the services have been tagged, you may easily resolve them all via the `tag
221222
return new ReportAggregator($app->tagged('reports'));
222223
});
223224

225+
226+
<a name="binding-tags"></a>
227+
### Binding Tags
228+
229+
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.
230+
231+
To bind services tagged as "reports" to a primitive (`array`) constructor argument named `$reports`:
232+
233+
class ReportAggregator
234+
{
235+
/** @var Report[] */
236+
public $reports;
237+
238+
public function __construct(array $reports)
239+
{
240+
$this->reports = $reports;
241+
}
242+
}
243+
244+
$this->app->when(ReportAggregator::class)
245+
->needs('$reports')
246+
->giveTagged('reports');
247+
248+
To bind services tagged as "reports" to a variadic constructor argument typed `Report`:
249+
250+
class ReportAggregator
251+
{
252+
/** @var Report[] */
253+
public $reports;
254+
255+
public function __construct(Report ...$reports)
256+
{
257+
$this->reports = $reports;
258+
}
259+
}
260+
261+
$this->app->when(ReportAggregator::class)
262+
->needs(Report::class)
263+
->giveTagged('reports');
264+
224265
<a name="extending-bindings"></a>
225266
### Extending Bindings
226267

0 commit comments

Comments
 (0)