Last active
October 1, 2019 07:02
-
-
Save lafraga93/395a37b569fafa00e0540eff1e05f380 to your computer and use it in GitHub Desktop.
Example 04 - DI com PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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