class Container implements ContainerInterface, ArrayAccess (View source)

Dependency Injection Container that supports different types of entries.

Properties

protected array[] $entries

Methods

__construct()

Container constructor.

void
setDelegate(ContainerInterface $delegate)

Sets the delegate container that is provided for dependency resolution.

string
getCacheFile(callable $encoder = null)

Returns PHP code for a cached container that can be loaded quickly on runtime.

void
addEntry(string $id, EntryInterface $type)

Adds an entry to the container.

mixed
get(string $id)

Returns value for the container entry with the given identifier.

bool
has(string $id)

Tells if the entry with the given identifier exists in the container.

bool
offsetExists(string $offset)

Tells if the entry with the given identifier exists in the container.

mixed
offsetGet(string $offset)

Returns value for the container entry with the given identifier.

void
offsetSet(string $offset, mixed $value)

Adds a container entry with a mixed type.

void
offsetUnset(string $offset)

Removes an entry from the container.

Details

__construct()

Container constructor.

void setDelegate(ContainerInterface $delegate)

Sets the delegate container that is provided for dependency resolution.

Parameters

ContainerInterface $delegate Delegate container to use for dependency resolution

Return Value

void

string getCacheFile(callable $encoder = null)

Returns PHP code for a cached container that can be loaded quickly on runtime.

Parameters

callable $encoder Encoder to turn the cached entry data into PHP code or null for default

Return Value

string The PHP code for the cached container

Exceptions

ContainerException If the container contains entries that cannot be cached

void addEntry(string $id, EntryInterface $type)

Adds an entry to the container.

Parameters

string $id The identifier for the container entry
EntryInterface $type The container entry to add

Return Value

void

Exceptions

ContainerException If trying to add an entry to an identifier that already exists

mixed get(string $id)

Returns value for the container entry with the given identifier.

Parameters

string $id The entry identifier to look for

Return Value

mixed The value for the entry

Exceptions

NotFoundExceptionInterface If the entry cannot be found
ContainerExceptionInterface If there are errors trying to load dependencies

bool has(string $id)

Tells if the entry with the given identifier exists in the container.

Parameters

string $id The entry identifier to look for

Return Value

bool True if an entry with the given identifier exists, false otherwise

bool offsetExists(string $offset)

Tells if the entry with the given identifier exists in the container.

Parameters

string $offset The entry identifier to look for

Return Value

bool True if an entry with the given identifier exists, false otherwise

mixed offsetGet(string $offset)

Returns value for the container entry with the given identifier.

Parameters

string $offset The entry identifier to look for

Return Value

mixed The value for the entry

Exceptions

NotFoundExceptionInterface If the entry cannot be found
ContainerExceptionInterface If there are errors trying to load dependencies

void offsetSet(string $offset, mixed $value)

Adds a container entry with a mixed type.

Parameters

string $offset The identifier for the container entry
mixed $value The value for the container entry

Return Value

void

Exceptions

ContainerException If trying to add an entry to an identifier that already exists

void offsetUnset(string $offset)

Removes an entry from the container.

Parameters

string $offset The identifier of the container entry to remove

Return Value

void