Container
in package
Class Container
A simple dependency injection container that manages singleton instances of classes.
Table of Contents
Properties
- $instances : array<string|int, mixed>
Methods
- resolve() : object|null
- Resolves and retrieves an instance of the specified class from the container.
- singleton() : object
- Retrieves or creates a singleton instance of the specified class.
Properties
$instances
private
static array<string|int, mixed>
$instances
= []
Methods
resolve()
Resolves and retrieves an instance of the specified class from the container.
public
static resolve(string $className) : object|null
Parameters
- $className : string
-
The fully qualified name of the class to resolve.
Return values
object|null —The instance of the specified class if it exists in the container, or null if not found.
singleton()
Retrieves or creates a singleton instance of the specified class.
public
static singleton(string $class[, string|callable|null $build = null ]) : object
This method ensures that only one instance of the specified class is created and reused throughout the application. If the instance does not already exist, it will be created based on the provided build parameter.
Parameters
- $class : string
-
The fully qualified class name of the singleton instance to retrieve or create.
- $build : string|callable|null = null
-
Optional. A parameter to customize the instance creation:
- If null, the class is instantiated with no arguments.
- If a string, the class is instantiated with the string as a constructor argument.
- If a callable, the callable is invoked to create the instance.
Tags
Return values
object —The singleton instance of the specified class.