Skip to content

Instantly share code, notes, and snippets.

@iansltx iansltx/Container.php
Last active Sep 12, 2019

Embed
What would you like to do?
Dependency Injection Sample Code - 2019
<?php
class Container implements Psr\Container\ContainerInterface {
protected $s = [];
function __set($k, $c) { $this->s[$k]=$c; }
function __get($k) { return $this->s[$k]($this); }
function get($k) { return $this->s[$k]($this); }
function has($k) { return isset($s[$k]); }
}
<?php
class Fizz { public function __toString() { return 'Fizz'; } }
class Buzz { public function __toString() { return 'Buzz'; } }
class FizzBuzz {
protected $fizz;
protected $buzz;
public function __construct(Fizz $fizz, Buzz $buzz) {
$this->fizz = $fizz; $this->buzz = $buzz;
}
public function __invoke(int $i) {
if (!($i % 15)) return $this->fizz . $this->buzz;
if (!($i % 3)) return (string) $this->fizz;
if (!($i % 5)) return (string) $this->buzz;
return (string) $i;
}
}
<?php
<<<CONFIG
packages:
- "league/container: ^3.2"
CONFIG;
class NeedsALogger {
private $logger;
public function __construct(Logger $logger) {
$this->logger = $logger;
}
}
class Logger {}
$c = new League\Container\Container;
$c->share('LoggerInterface', 'Logger');
$c->delegate(new League\Container\ReflectionContainer);
var_dump($c->get('NeedsALogger')); // NeedsALogger w\Logger
<?php
<<<CONFIG
packages:
- "league/container: ^3.2"
CONFIG;
class NeedsALogger {
private $logger;
public function __construct(Logger $logger) {
$this->logger = $logger;
}
}
class Logger {}
$c = new League\Container\Container;
$c->share('LoggerInterface', 'Logger');
$c->add('myClass', 'NeedsALogger')
->addArgument('LoggerInterface');
var_dump($c->get('myClass')); // NeedsALogger
<?php
<<<CONFIG
packages:
- "php-di/php-di: ^6.0"
CONFIG;
use function DI\create;
class NeedsALogger {
private $logger;
public function __construct(Logger $logger) {
$this->logger = $logger;
}
}
class Logger {}
$c = new \DI\Container();
$c->set('LoggerInterface', create('Logger'));
// returns a NeedsALogger
var_dump($c->get('NeedsALogger'));
<?php
<<<CONFIG
packages:
- "php-di/php-di: ^6.0"
CONFIG;
use function DI\autowire;
class NeedsALogger {
private $logger;
public function __construct(Logger $logger) {
$this->logger = $logger;
}
}
class Logger {}
$builder = new \DI\ContainerBuilder();
$builder->enableCompilation('/tmp');
$builder->addDefinitions([
'LoggerInterface' => autowire('Logger'),
'NeedsALogger' => autowire()
]);
$c = $builder->build();
var_dump($c->get('NeedsALogger'));
<?php
<<<CONFIG
packages:
- "php-di/php-di: ^6.0"
CONFIG;
require 'FizzBuzz.php';
$c = new \DI\Container();
foreach (range(1, 15) as $i) {
echo $c->get('fizzBuzz')($i) . "\n";
}
<?php
<<<CONFIG
packages:
- "psr/container: ^1.0"
CONFIG;
require 'Container.php';
require 'FizzBuzz.php';
$c = new Container;
$c->fizz = function() { return new Fizz; };
$c->buzz = function() { return new Buzz; };
$c->fizzBuzz = function($c) {
return new FizzBuzz($c->fizz, $c->buzz);
};
foreach (range(1, 15) as $i) {
echo $c->get('fizzBuzz')($i) . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.