backspace / container
The package enables binding classes to specific implementations and resolving their dependencies.
README
A lightweight dependency injection (DI) container for PHP: bind abstractions to implementations and resolve dependencies automatically via the constructor.
Installation
composer require backspace/container
The package has no extra runtime dependencies (empty require).
Quick start
<?php
use Backspace\Framework\Container;
require __DIR__ . '/vendor/autoload.php';
$container = new Container();
// Instantiate a class (if it has a constructor, types are resolved recursively)
$service = $container->make(MyService::class);
Registering bindings (bind)
Abstraction → concrete class
If the second argument is a string class name, the container will build that class when the first is requested:
interface LoggerInterface {}
class FileLogger implements LoggerInterface {}
$container->bind(LoggerInterface::class, FileLogger::class);
$logger = $container->make(LoggerInterface::class); // FileLogger instance
Factory (Closure)
Full control over object creation; the container is passed into the closure:
$container->bind(CacheInterface::class, function (Container $c) {
return new RedisCache($_ENV['REDIS_DSN']);
});
$cache = $container->make(CacheInterface::class);
Binding a class to itself
If the second argument is omitted (null), resolving that class calls make() for the same name—useful to register a class before it is requested as a dependency:
$container->bind(AppConfig::class);
$config = $container->make(AppConfig::class);
Constructor autowiring (make)
If there is no binding for a class, reflection is used: for each constructor parameter with a class type, make() is called:
class Database {}
class UserRepository
{
public function __construct(
private Database $db
) {}
}
$repo = $container->make(UserRepository::class);
// Database is created and injected into UserRepository
Classes without a constructor are created as new ClassName().
Invoking methods with dependency injection (call)
You can call an instance method or a static class method; method parameters with class types are resolved the same way as constructor parameters:
class ReportController
{
public function __construct(
private ReportService $reports
) {}
public function index(Request $request): string
{
return $this->reports->build($request);
}
}
$controller = $container->make(ReportController::class);
// Instance method: first argument is the object
$html = $container->call($controller, 'index');
// Static method: first argument is the class name
$html = $container->call(ReportController::class, 'someStaticAction');
Limitations
- Constructor/method parameters without a class type hint (scalars, arrays, etc.) are not filled automatically in the current implementation—supply them via a factory in
bindor adjust signatures. - Each
make()for a closure binding creates a new instance (there is no separate “singleton” mode in the API).
License
MIT