Skip to content

Tags ​

Tags make it possible to expand the ways in which collections can be passed as parameters to methods and functions. Elements from a container that are marked with a tag of the same name are gathered into a collection.

Dependency injection is applicable to parameters of the following type:

  • iterable
    • \Traversable
      • \Iterator
  • \ArrayAccess
  • \Psr\Container\ContainerInterface
  • array (the argument false must be passed for the $isLazy parameter)
  • \ArrayAccess & \Iterator & \Psr\Container\ContainerInterface

Any definition within a container can be marked with one or more tags. Each tag can contain metadata.

Объявить теги определения контейнера можно используя:

Dependency injection of a collection of elements into a parameter can be declared using:

IMPORTANT

PHP classes that have not been explicitly registered in the container will be ignored during tag-based lookups; therefore, you must ensure that these classes are added to the container using one of the following methods:

Lazy Collection ​

An element of a lazy collection is initialized only at the moment it is actually accessed; this allows the creation of an object's dependencies to be deferred until the point they are truly needed, rather than any earlier.

A "lazy collection" is defined via the $isLazy parameter in the diTaggedAs() helper function or the TaggedAs attribute.

WARNING

A lazy collection cannot be applied to a parameter of type array. For parameters of type array, the $isLazy parameter must be set to false.

Identifier of an item in a collection ​

By default, an injected collection uses the container ID[1] of each element as its identifier.

This behavior can be modified using the $useKeys, $key, and $keyDefaultMethod parameters — available via the diTaggedAs() helper function or the TaggedAs attribute.

Element identifiers within a collection can be arbitrary strings or integers.

For more information, see the documentation section on "Collection Element Identifiers".

Section Contents ​


  1. A container ID is a string name (or a class/interface name) used to register a service or object within a container and to retrieve it via the get() method. ↩︎