P.H.'s Dependency Injection Container Library
โณ
Summary The "P.H.'s DIC" is a simple and lightweight PHP 7.1+ Dependency Injection Container's library which lets you manage your dependencies easily for your next great project
โ
Requirements
๐
Installation composer require ph-7/dependency-injection-container
๐ฎ
Usage Register your new DIC as below (FYI, for this example I use the Symfony's HttpFoundation Request).
For the first example, let's create your provider class with an anonymous class that implements the \PierreHenry\Container\Providable
interface.
use PierreHenry\Container\Container;
use PierreHenry\Container\Providable;
use Symfony\Component\HttpFoundation\Request;
$container = new Container();
// Register your container
$container->register(
'example.symfony.httprequest',
new class implements Providable
{
public function getService(): Request
{
return Request::createFromGlobals();
}
}
);
// Retrieve the container
$httpRequest = $container->get('example.symfony.httprequest');
// Use it
$request = $httpRequest->request; // $_POST body params
if ($request->get('get_var')) {
echo '$_POST["get var"] exists';
} else {
echo '"get_var" has not been requested';
}
Another Example...
use DateTime;
use DateTimeZone;
use PierreHenry\Container\Container;
use PierreHenry\Container\Providable;
$container = new Container();
$container->register(
'stubs.date.datetime',
new class implements Providable
{
public function getService(): DateTime
{
return new DateTime('now', new DateTimeZone('America/Chicago'));
}
}
);
// Retrieve the container
$date = $container->get('stubs.date.datetime');
// Use it
echo $date->format('m-d-Y H:i:s');
๐ง
Inspired By... This project is highly inspired by my DIC I built for another side-project.
๐ค
...Who Am I...? Hi
You can keep in touch at hi {{AT}} ph7 [[D0T]] me!
๐
Passion Drives Me! Love programming!
โ
License Under GNU GPL v3 or later.