Skip to content

Instantly share code, notes, and snippets.

@lafraga93
Last active October 1, 2019 07:02
Show Gist options
  • Save lafraga93/395a37b569fafa00e0540eff1e05f380 to your computer and use it in GitHub Desktop.
Save lafraga93/395a37b569fafa00e0540eff1e05f380 to your computer and use it in GitHub Desktop.
Example 04 - DI com PHP
<?php
declare(strict_types=1);
namespace App\Example;
use DI\ContainerBuilder;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
final class FourthExample
{
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function something(): void
{
//...
$this->logger->debug('fourth example debug description');
}
}
$builder = new ContainerBuilder();
$builder->addDefinitions([
LoggerInterface::class => new Logger('My_Logger', [
new StreamHandler('logs/debug.log', Logger::DEBUG),
]),
]);
$container = $builder->build();
$exampleController = $container->get('\App\Example\FourthExample');
$exampleController->something();
// OR
$container->call(['\App\Example\FourthExample', 'something'], []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment