Neuron-PHP

Container implements IContainer

Dependency injection container with auto-wiring

Implements PSR-11 compatible container interface with automatic dependency resolution using reflection.

Table of Contents

Interfaces

IContainer
PSR-11 compatible dependency injection container interface

Properties

$_bindings  : array<string|int, mixed>
Registered type bindings (interface => concrete class)
$_instances  : array<string|int, mixed>
Resolved singleton instances
$_singletons  : array<string|int, mixed>
Registered singleton factories

Methods

bind()  : void
Bind an abstract type to a concrete implementation
clear()  : void
Clear all bindings and instances (useful for testing)
get()  : mixed
Finds an entry of the container by its identifier and returns it.
getBindings()  : array<string|int, mixed>
Get all registered bindings
has()  : bool
Returns true if the container can return an entry for the given identifier.
instance()  : void
Register an existing instance as a singleton
isBound()  : bool
Check if a binding exists
make()  : object
Resolve and instantiate a class with automatic dependency injection
singleton()  : void
Register a singleton (shared instance) in the container
resolveDependencies()  : array<string|int, mixed>
Resolve method/constructor dependencies

Properties

$_bindings

Registered type bindings (interface => concrete class)

private array<string|int, mixed> $_bindings = []

$_instances

Resolved singleton instances

private array<string|int, mixed> $_instances = []

$_singletons

Registered singleton factories

private array<string|int, mixed> $_singletons = []

Methods

bind()

Bind an abstract type to a concrete implementation

public bind(string $abstract, string $concrete) : void
Parameters
$abstract : string

Interface or abstract class name

$concrete : string

Concrete class name

clear()

Clear all bindings and instances (useful for testing)

public clear() : void

get()

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

public get(string $id) : mixed
Parameters
$id : string

Identifier of the entry to look for (typically a class name or interface)

Return values
mixed

Entry.

getBindings()

Get all registered bindings

public getBindings() : array<string|int, mixed>
Return values
array<string|int, mixed>

has()

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

public has(string $id) : bool
Parameters
$id : string

Identifier of the entry to look for.

Return values
bool

instance()

Register an existing instance as a singleton

public instance(string $abstract, object $instance) : void
Parameters
$abstract : string

Interface or class name

$instance : object

The instance to register

isBound()

Check if a binding exists

public isBound(string $abstract) : bool
Parameters
$abstract : string
Return values
bool

make()

Resolve and instantiate a class with automatic dependency injection

public make(string $class[, array<string|int, mixed> $parameters = [] ]) : object
Parameters
$class : string

Fully qualified class name

$parameters : array<string|int, mixed> = []

Optional parameters to override auto-wiring

Return values
object

Instance of the requested class

singleton()

Register a singleton (shared instance) in the container

public singleton(string $abstract, callable $factory) : void
Parameters
$abstract : string

Interface or class name

$factory : callable

Factory function that creates the instance

resolveDependencies()

Resolve method/constructor dependencies

private resolveDependencies(array<string|int, ReflectionParameter$parameters[, array<string|int, mixed> $primitives = [] ]) : array<string|int, mixed>
Parameters
$parameters : array<string|int, ReflectionParameter>

Constructor/method parameters

$primitives : array<string|int, mixed> = []

User-provided primitive values (name => value)

Tags
throws
ContainerException

If dependency cannot be resolved

Return values
array<string|int, mixed>

Resolved dependencies


        
On this page

Search results