b2pweb/bdf-instantiator
b2pweb/bdf-instantiator is a small PHP utility for instantiating objects, useful in factories, DI containers, and testing. It focuses on creating class instances with minimal boilerplate, helping streamline object construction in Laravel and other PHP apps.
A basic object instantiator.
$ composer require b2pweb/bdf-instantiator
Basic usage when resolving from container definition.
use Bdf\Instantiator\Instantiator;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
/** @var ContainerInterface $container */
$container->add(Logger::class, LoggerInterface::class);
$instantiator = new Instantiator($container);
$instantiator->make(LoggerInterface::class);
The instantiator resolve the dependencies of a method based on container definitions.
use Bdf\Instantiator\Instantiator;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
class Foo
{
public $logger;
/**
* Foo constructor.
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
/** @var ContainerInterface $container */
$container->add(Logger::class, LoggerInterface::class);
$instantiator = new Instantiator($container);
$foo = $instantiator->make(Foo::class);
var_dump($foo->logger); // Logger
Distributed under the terms of the MIT license.
How can I help you explore Laravel packages today?