Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DI Container Museum@PHP

DI Container Museum@PHP

YAP(achimon)C::Asia Hachioji 2016

Sota Sugiura

July 01, 2016
Tweet

More Decks by Sota Sugiura

Other Decks in Technology

Transcript

  1. 6TFSQIQ <?php class User { protected $db; public function __construct()

    { $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } }
  2. <?php class User { protected $db; public function __construct() {

    $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ %#Ϋϥεͷ"1*มߋͷӨڹΛड͚Δ
  3. <?php class User { protected $db; public function __construct() {

    $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ σʔλϕʔεมߋ࣌ʹ Ҿ਺Λॻ͖׵͑ͳ͚Ε͹͍͚ͳ͍
  4. <?php class User { protected $db; public function __construct() {

    $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } 6TFSQIQ ͜ͷ஋ΛͲ͏΍ͬͯϞοΫ͢Δͷʁ
  5. 6TFSQIQ <?php class User { protected $db; public function __construct()

    { $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } }
  6. 6TFSQIQ <?php class User { protected $db; public function __construct()

    { $this->db = new DB('mysql'); } public function getUsers() { return $this->db->table('users')->selectAll(); } } ͜ͷॲཧ͕ݪҼͬΆ͍
  7. 6TFSQIQ <?php class User { protected $db; public function __construct($db)

    { $this->db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } }
  8. 6TFSQIQ <?php class User { protected $db; public function __construct(DB

    $db) { $this->db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } } 5ZQF)JOU
  9. .BJOQIQ <?php // Success $user = new User(new DB('mysql')); //

    PHP Warning: Uncaught TypeError $badUser = new User('DB');
  10. 6TFSQIQ <?php class User { protected $db; public function __construct(DBInterface

    $db) { $this->db = $db; } public function getUsers() { return $this->db->table('users')->selectAll(); } } *OUFSGBDF
  11. .BJOQIQ <?php // Success $user = new User(new DB('mysql')); //

    PHP Warning: Uncaught TypeError $badUser = new User(new ViolateDB('mysql'));
  12. $POUSPMMFSQIQ <?php class Controller { public function getIndex() { $service

    = new UserService( new UserRepository( new DB('mysql'), new Cache('redis') ), new BoardRepository( new DB('mysql'), new Cache('memcached') ) ); return $service->getIndexPage(); } }
  13. $POUSPMMFSQIQ <?php class Controller { public function getIndex() { $service

    = new UserService( new UserRepository( new DB('mysql'), new Cache('redis') ), new BoardRepository( new DB('mysql'), new Cache('memcached') ) ); return $service->getIndexPage(); } } ᶃෳࡶ͗͢Δ
  14. $POUSPMMFSQIQ <?php class Controller { public function getIndex() { $service

    = new UserService( new UserRepository( new DB('mysql'), new Cache('redis') ), new BoardRepository( new DB('mysql'), new Cache('memcached') ) ); return $service->getIndexPage(); } } ᶃෳࡶ͗͢Δ ᶄ݁ہOFXͯ͠Δ
  15. 1)1%*$POUBJOFST w QJNQMF w BVSBEJ w 1)1%* w %JDF w

    UIFQIQMFBHVF w TZNGPOZ w "VSZO w [FOETFSWJDFNBOBHFS w BOEFUD
  16. 1)1%*$POUBJOFST w QJNQMF w BVSBEJ w 1)1%* w %JDF w

    UIFQIQMFBHVF w TZNGPOZ w "VSZO w [FOETFSWJDFNBOBHFS w BOEFUD ଟ͍
  17. LFZWBMVF QJNQMF <?php use Pimple\Container; $container = new Container(); //

    Register instance. $container['session'] = function ($c) { return new App\Session('SESSION_ID'); };
  18. "OOPUBUJPO 1)1%* <?php class Yapc { /** * @Inject *

    @var Talk */ private $talk; /** * @Inject * @param Talk $talk */ public function __construct($talk) { } }
  19. 4BNQMF ;FOE <?php use Interop\Container\ContainerInterface; use Zend\ServiceManager\Factory\InvokableFactory; use Zend\ServiceManager\ServiceManager; use

    stdClass; $serviceManager = new ServiceManager([ 'factories' => [ stdClass::class => InvokableFactory::class, MyObject::class => function (ContainerInterface $c, $name) { return new MyObject($c->get('obj')); }, ] ]);
  20. 4BNQMF BVSBEJ <?php $builder = new Aura\Di\ContainerBuilder; // Use auto

    wiring $di = $builder->newInstance($builder::AUTO_RESOLVE); // Get instance $userService = $di->newInstance('UserService');
  21. (FUCZLFZOBNF QJNQMF <?php use Pimple\Container; $container = new Container(); //

    Register instance. $container['session'] = function ($c) { return new App\Session('SESSION_ID'); }; // Get instance. $session = $container['session'];
  22. (FUCZDMBTTOBNF BVSBEJ <?php $builder = new Aura\Di\ContainerBuilder; $di = $builder->newInstance();

    // Register $di->params['Example']['foo'] = new Foo(); // Get instance $example = $di->newInstance('Example');