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

PHPのDI、attributesとこれから / PHP DI with attributes

PHPのDI、attributesとこれから / PHP DI with attributes

2021年3月28日、PHPerKaigi 2021・Day 2での発表資料です。
https://phperkaigi.jp/2021/

YAMAOKA Hiroyuki

March 26, 2021
Tweet

More Decks by YAMAOKA Hiroyuki

Other Decks in Programming

Transcript

  1. PHP DI


    attributes
    2021 3 28 / PHPerKaigi
    2 0
    2
    1
    Day
    2

    View full-size slide

  2. - / @hiro_y


    - PHP Node.js Web


    -


    - CTO

    View full-size slide

  3. -


    -


    -


    - iruca mimemo
    👨💻👨💻

    View full-size slide

  4. - PHPerKaigi
    2
    0 20
    Day
    0


    - 2020 2 9


    - PHP



    https://speakerdeck.com/hiro_y/about-php-annotations


    https://www.youtube.com/watch?v=TdHDqOeCUW
    4

    View full-size slide

  5. 🎉 PHP
    8
    .
    0
    🎉


    2020 11

    View full-size slide

  6. https://www.php.net/releases/
    8
    .
    0
    /ja.php

    View full-size slide

  7. DI
    - Dependency:


    - Injection:
    class Client {

    public function __construct(private Service $service) {}

    }

    View full-size slide

  8. DI
    - Maple https://ja.osdn.net/projects/maple/


    - S
    2
    Container.PHP
    5


    http://s
    2
    container.php
    5
    .seasar.org/
    2
    .
    0
    /ja/index.html


    - PHP
    5

    View full-size slide

  9. - Java DI


    - Struts
    1
    Spring Seasar
    2


    - PHP
    5

    - DI

    View full-size slide

  10. PHP DI
    - PHP DI


    -


    - Web :


    - TDD CI :

    View full-size slide

  11. PHP
    -


    - interface


    - autoload require


    - TDD CI

    View full-size slide

  12. DI
    - execute($request)


    :


    - show(Request $request)


    : routing


    - show(ServerRequestInterface $request)


    interface :

    View full-size slide

  13. - IDE PhpStorm VSCode


    - interface


    - APC OPcache


    -

    View full-size slide

  14. - Symfony Laravel CakePHP


    - DI


    -
    🕵

    View full-size slide

  15. Symfony
    - Service Container


    https://symfony.com/doc/
    5
    .
    2
    /components/dependency_injection.html


    - Dependencyinjection component


    - PSR-
    1
    1

    - 2

    View full-size slide

  16. Laravel
    - Service Container


    https://laravel.com/docs/
    8
    .x/container


    - Illuminate\Container\Container


    - PSR-
    1
    1

    - Facades

    View full-size slide

  17. : PSR-
    1
    1
    - PSR-
    1
    1
    : Container Interface


    https://www.php-
    fi
    g.org/psr/psr-
    1 1
    /


    -


    - :


    View full-size slide

  18. PHP-DI
    - DI https://php-di.org/


    - PSR-
    1
    1

    - Slim middleware


    View full-size slide

  19. PHP-DI
    7
    - 2021 3 10 : beta
    3

    - beta


    - Attributes


    - PHP
    8
    PHP-DI
    6

    View full-size slide

  20. PHP-DI
    7
    - @Inject disabled #[Inject]


    -


    -


    - PhpDocReader

    View full-size slide

  21. Attributes
    /**


    * @Inject({"db.host", "db.name"})


    */


    public function __construct($param1, $param2)
    use DI\Attribute\Inject;


    #[Inject('db.host', 'db.name')]


    public function __construct($param1, $param2)

    View full-size slide

  22. @Inject
    - doctrine/annotations


    - Re
    fl
    ection DocComment


    - DocLexer


    -

    View full-size slide

  23. #[Inject]
    - PHP
    8
    Attributes
    namespace DI\Attribute;


    use Attribute;


    #[Attribute(Attribute::TARGET_PROPERTY |
    Attribute::TARGET_METHOD | Attribute::TARGET_PARAMETER)]


    final class Inject


    {...

    View full-size slide

  24. Inject
    -


    - Inject


    - Autowire


    -

    View full-size slide

  25. Autowire
    - Inject


    -


    - Zero Con
    fi
    guration


    - PHP-DI DI

    View full-size slide

  26. Autowire
    -


    - class_exists / interface_exists


    -


    - interface

    View full-size slide

  27. DI
    -


    -


    - Service


    -

    View full-size slide

  28. DI
    -


    - PSR


    - attributes


    View full-size slide