Container class

class Slick\Di\Container

Container is where your dependencies live. It holds a list of DefinitionInterface objects that can be resolved into objects or values.

Slick\Di\Container::get($id)

Finds an entry of the container by its identifier and returns it.

Parameters:
  • $id (string) – Identifier of the entry to look for.
Throws:

NotFoundException – No entry was found for this identifier.

Returns:

An object or value that was stored or has its definition under the provided $id identifier.

Slick\Di\Container::has($id)

Returns true if the container can return an entry for the given identifier

Parameters:
  • $id (string) – Identifier of the entry to look for.
Returns:

True if container can return an entry or false otherwise.

Slick\Di\Container::register($name, $definition[, $scope, $params])

Adds a definition or a value to the container with the $name as identifier.

Parameters:
  • $name (string) – Identifier where the entry will be stored in.
  • $definition (mixed) – The definition or value to store.
  • $scope (string|Scope) – The resolution scope. Please see Definitions page for details. Defaults to Scope::SINGLETON.
  • $params (array) – An array of arguments to pass to callable (Factory) definitions. Defaults to an empty array.
Returns:

Container itself. Useful for chaining more container calls.

Tip

$definition can be any value and the container will evaluate it in order to determine what strategy it will use to resolve it latter on. The possibilities are:

  • scalar|objects: scalar values or objects are store as Value definition;
  • @<identifier>: this is an Alias definition that will point to the definition stored under the identifier name;
  • callable: a callable is stored as Factory definition. It will be executed when Container::get() is called for the first time and the result value will be returned in the subsequent calls. $params will be passed when executing the callable;
  • Slick\Di\DefinitionInterface: Definition interface handle the entry resolution. In this case the container will return the resolved value of the definition.
Slick\Di\Container::make($className[, ...$arguments])

Creates an instance of provided class injecting its dependencies.

Parameters:
  • $className (string) – The class name that the container will use to create the object.
  • $arguments (array) – An optional list of arguments to pass to the constructor.
Returns:

An object from the type passed in the $className argument.

Tip

If you create a class that implements the Slick\Di\ContainerInjectionInterface all the $arguments that you may pass to this method will be ignored as the container will call the create() method and pass himself as an argument to that method. Please see ContainerInjectionInterface reference

All other classes will be resolved with an Object definition and if you use any @inject annotation the correspondent dependency will be injected.

Entry alias (@alias) can be used in the arguments to reference injected dependencies.