sabroan / di-reflection
An auto-wiring reflection library for PSR-11
Requires
- php: >=8.5
- psr/container: ^2
Requires (Dev)
- ergebnis/composer-normalize: ^2.54
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^13
- roave/security-advisories: dev-latest
- vimeo/psalm: ^7@beta
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-19 09:47:47 UTC
README
A reflection wrapper library for dependency injection (DI) containers and auto-wiring engines.
Built on PHP's native Reflection API, it extends standard reflection capabilities to resolve dependencies through PSR-11 containers, automatically instantiate missing dependencies, match complex type declarations, and support lazy initialization using Lazy Ghost objects.
Features
-
PSR-11 Container Integration: Resolves typed class dependencies through any PSR-11-compatible container.
-
Automatic Dependency Resolution: Automatically instantiates eligible dependencies that are not available in the container, recursively resolving their dependencies where possible.
-
Lazy Ghost Support: Leverages
ReflectionClass::newLazyGhost()for deferred, memory-efficient initialization of eligible classes. -
Comprehensive Type Matching: Supports named, union, and intersection types, including relative type declarations such as
self,static, andparent.
Installation
Install the package via Composer:
composer require sabroan/di-reflection
Quick Start
Resolving Classes
use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Sabroan\DI\Reflection\ReflectionClass; readonly class LoggerService { public function __construct( public LoggerInterface $logger ) { $logger->log('Hello, world!'); } } $class = new ReflectionClass(LoggerService::class); $constructor = $class->getConstructor(); $service = (null === $constructor) ? $class->newInstance() : $class->newLazyGhost( static fn (object $ghost) => $ghost->__construct( /** @var ContainerInterface $container */ ...$constructor->getParameters($container) ) ); $service->logger->log('WoW');
Resolving Methods
use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Sabroan\DI\Reflection\ReflectionClass; readonly class Greeter { public function greet(LoggerInterface $logger) { $logger->log('Hello, world!'); } } $method = new ReflectionMethod(Greeter::class, 'greet'); /** @var ContainerInterface $container */ $arguments = $method->getParameters($container); $method->invokeArgs(new Greeter(), $arguments);
Resolving Functions
use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Sabroan\DI\Reflection\ReflectionFunction; $function = new ReflectionFunction( static function (LoggerInterface $logger): void { $logger->log('Hello, world!'); } ); /** @var ContainerInterface $container */ $arguments = $method->getParameters($container); $function->invokeArgs($arguments);
Resolving Parameters
use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Sabroan\DI\Reflection\ReflectionParameter; $closure = static function (LoggerInterface $logger): void { $logger->log('Hello, world!'); }; $parameter = new ReflectionParameter($closure, 'logger'); /** @var ContainerInterface $container */ $logger = $parameter->getDefaultValue($container); assert($logger instanceof LoggerInterface); $closure($logger);
License
This package is released under the MIT License. See the LICENSE file for details.