Simple Php dependency resolver
Simple PSR-11 compliant dependency injection container.
Around the the web there are many examples to be found for simple PHP DI Containers and Resolvers.
From all the inspiration I have written a version to my own personal liking.
Feel free to use it in your project or take it as inspiration for your own version.
For now only the object definition has been implemented but more will follow.
$ composer require jascha030/dic
Short example:
Class UserService with dependency User.
class UserService
{
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function printUserName()
{
echo "My name is " . $this->user->name;
}
}
$container = new PsrServiceContainer(); // Instantiate container
$userService = $container->get(UserService::class); // Get service or class instance.
$userService->printUserName(); // Outputs: My name is Jeff
Full example in src/example.php