bind(string $abstract, $concrete = null, bool $shared = false): voidRegister a binding with the container.
Parameters:
$abstract (string): The interface or class name to bind$concrete (string|Closure|null): The concrete implementation, closure, or null for self-binding$shared (bool): Whether the binding is a singleton (default: false)Returns: void
Example:
$container->bind(LoggerInterface::class, FileLogger::class);
$container->bind(ApiClient::class, fn($c) => new ApiClient());
singleton(string $abstract, $concrete = null): voidRegister a singleton binding (only one instance will be created).
Parameters:
$abstract (string): The interface or class name to bind$concrete (string|Closure|null): The concrete implementation, closure, or null for self-bindingReturns: void
Example:
$container->singleton(DatabaseService::class);
$container->singleton(CacheService::class, fn($c) => new RedisCache());
instance(string $abstract, object $instance): voidBind an existing instance to the container.
Parameters:
$abstract (string): The interface or class name to bind$instance (object): The concrete instanceReturns: void
Example:
$config = new Config(['debug' => true]);
$container->instance(ConfigInterface::class, $config);
resolve(string $abstract): mixedResolve a service from the container.
Parameters:
$abstract (string): The interface or class name to resolveReturns: mixed - The resolved instance
Example:
$logger = $container->resolve(LoggerInterface::class);
get(string $abstract): mixedAlias for resolve(). Resolve a service from the container.
Parameters:
$abstract (string): The interface or class name to resolveReturns: mixed - The resolved instance
Example:
$logger = $container->get(LoggerInterface::class);
$service = $container->get(MyService::class);
has(string $id): boolCheck if a binding exists in the container.
Parameters:
$id (string): The interface or class name or string to checkReturns: bool - True if binding exists, false otherwise
Example:
if ($container->has(LoggerInterface::class)) {
$logger = $container->resolve(LoggerInterface::class);
}
forbidContainerInjection(false): voidPrevent the forbidden container from being injected as a dependency.
Parameters: None
Returns: void
Example:
$container->forbidContainerInjection(false);
// This will now not throw an exception
class BadService {
public function __construct(Container $container) {}
}