MCP PHP SDK

Container implements ContainerInterface

FinalYes

A basic PSR-11 container implementation with simple constructor auto-wiring.

Supports instantiating classes with parameterless constructors or constructors where all parameters are type-hinted classes/interfaces known to the container, or have default values. Does NOT support scalar/built-in type injection without defaults.

Tags
author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Interfaces

ContainerInterface

Properties

$instances  : array<string, object>
$resolving  : array<string, bool>

Methods

get()  : mixed
Finds an entry of the container by its identifier and returns it.
has()  : bool
Returns true if the container can return an entry for the given identifier.
set()  : void
Adds a pre-built instance or a factory/binding to the container.
resolveParameter()  : mixed
Attempts to resolve a single constructor parameter.

Properties

$instances

private array<string, object> $instances = []

Cache for already created instances (shared singletons)

$resolving

private array<string, bool> $resolving = []

Track classes currently being resolved to detect circular dependencies

Methods

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 (usually a FQCN)

Tags
throws
NotFoundExceptionInterface

no entry was found for this identifier

throws
ContainerExceptionInterface

Error while retrieving the entry (e.g., dependency resolution failure, circular dependency).

Return values
mixed

entry

has()

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

public has(string $id) : bool

Checks explicitly set instances and if the class/interface exists. Does not guarantee get() will succeed if auto-wiring fails.

Parameters
$id : string
Return values
bool

set()

Adds a pre-built instance or a factory/binding to the container.

public set(string $id, object $instance) : void

This basic version only supports pre-built instances (singletons).

Parameters
$id : string
$instance : object

resolveParameter()

Attempts to resolve a single constructor parameter.

private resolveParameter(ReflectionParameter $parameter, string $consumerClassId) : mixed
Parameters
$parameter : ReflectionParameter
$consumerClassId : string
Tags
throws
ContainerExceptionInterface

if a required dependency cannot be resolved


        
On this page

Search results