\Omega\Container Container

Container class.

The Container class provides a basic dependency injection container for managing class instances and their dependencies.

Summary

Methods
Properties
Constants
has
get
alias
resolve
call
remove
clear
getBindings
No public properties found
No public constants found
No protected methods found
No protected properties found
No protected constants found
getReflector
bindings
resolved
No private constants found

Properties

$bindings

$bindings : array

Binding class.

Type

array<string, callable> — Holds an array of class alias to factory closure bindings.

$resolved

$resolved : array

Resolved class.

Type

array<string, mixed> — Holds an array of resolved class instances by alias.

Methods

has()

has(string  $alias) : bool

Determine if the container has a binding for the given alias.

Parameters

string $alias

The class alias or key to check.

Returns

bool —

Returns true if the alias exists in the container bindings, false otherwise.

get()

get(string  $alias) : mixed

Retrieve an instance from the container by alias.

Parameters

string $alias

The class alias or key.

Throws

\Omega\Container\Exception\KeyNotFoundException

if the alias is not found in the container.

Returns

mixed —

The resolved class instance.

alias()

alias(string  $alias, callable  $factory) : $this

Bind the class.

Parameters

string $alias

Holds the class alias or key.

callable $factory

Holds a closure that defines how to create the class instance.

Returns

$this —

resolve()

resolve(string  $alias) : mixed

Resolve the container.

Parameters

string $alias

Holds the class alias or key.

Throws

\Omega\Container\Exception\KeyNotFoundException

if the key is not bound.

Returns

mixed —

Return the resolved class instance.

call()

call(callable|array{0: object|string, 1: string}  $callable, array  $parameters = []) : mixed

Call a callable with dependency injection.

Parameters

callable|array{0: object|string, 1: string} $callable

Holds the callable function or method.

array $parameters

Holds an associative array of additional parameters to pass.

Throws

\ReflectionException

if the callable cannot be reflected.

\Omega\Container\Exception\DependencyResolutionException

if a dependency cannot be resolved.

\InvalidArgumentException

if the callable is not invocable.

Returns

mixed —

Return the result of the callable.

remove()

remove(string  $alias) : bool

Remove the binding for the given alias.

Parameters

string $alias

The class alias or key to remove.

Returns

bool —

Returns true if the alias was found and removed, false otherwise.

clear()

clear() : void

Clear all bindings from the container.

This method removes all registered aliases and resolved instances.

Returns

void —

getBindings()

getBindings() : array<string,callable>

Get all bindings from the container.

Returns

array

Returns an array of all registered bindings.

getReflector()

getReflector(callable|array{0: object|string, 1: string}  $callable) : \ReflectionMethod|\ReflectionFunction

Get reflector for the given callable.

Parameters

callable|array{0: object|string, 1: string} $callable

Holds the callable function or method.

Throws

\ReflectionException

if the callable cannot be reflected.

\InvalidArgumentException

if an unsupported callable type is provided.

Returns

\ReflectionMethod|\ReflectionFunction —

Return the reflection object for the callable.