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

PHP7.2で始める型宣言 // Type declaration with PHP 7.2

PHP7.2で始める型宣言 // Type declaration with PHP 7.2

2017/12/06 社内テックミーティング

Yoshihide TANIGUCHI

December 06, 2017
Tweet

More Decks by Yoshihide TANIGUCHI

Other Decks in Programming

Transcript

  1. 1)1Ͱ࢝ΊΔܕએݴ
    &$5&$).&&5*/(

    !SBWFMMM

    View full-size slide

  2. w ࣾ಺Ͱ1)1Yͷػӡ͕ͲΜͲΜߴ·͍ͬͯ·͢
    w ॲཧ͕ߴ଎Խ͢Δ͚ͩͰͳ͘ɺͰ͖Δ͜ͱ͕૿͑·͢
    w ͦͷதͰ΋ɺ1)1͔Β෇͖߹͍ํ͕มΘΔɾม͑Δ
    ͱྑ͍֓೦͕͋Γ·͢
    w ߴ඼࣭ͳιϑτ΢ΣΞΛ࡞Γ͍ͨɺ1)1Ͱɻ

    View full-size slide

  3. w ൃදͷ಺༰Λ͖ͬͪΓ֮͑Δඞཁ͸ͳ͍Ͱ͢
    w ͜ͷ৔ʹ͍Δਓ͕কདྷ1)1ͷίʔυΛॻ͘ͱ͖ʹ

    ʮͦ͏͍͑͹͍͋͋͏ػೳ͕͋ͬͨؾ͕͢Δʯͱ

    ࢥ͍ग़ͯ͠΋Β͑Ε͹े෼
    w ػೳͷ࢖͍ํ͸ௐ΂ΒΕΔ͚Ͳ

    ػೳͷଘࡏ͸ೳಈతʹ௥ͬͯΔਓҎ֎ؾ͚ͮͳ͍

    View full-size slide

  4. ࠓճઆ໌͢Δػೳɿܕએݴ

    View full-size slide

  5. class Calculator
    {
    public static function pow3(int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3(3);
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // => 27

    View full-size slide

  6. ܕએݴ
    w ݹ͘͸λΠϓώϯςΟϯάͱ΋
    w ϝιου΍ؔ਺ͷҾ਺ɾฦΓ஋ͷܕΛࢦఆ͢Δػೳ
    w 1)1͔Βଘࡏ͕ͨ͠ɺόʔδϣϯ্͕͕ΔʹͭΕɺ

    ΑΓଟ͘ͷܕ͕ࢦఆͰ͖ΔΑ͏ʹͳͬͨ
    w ֤όʔδϣϯͰࢦఆͰ͖Δܕʹ͍ͭͯ͸

    υΩϡϝϯτ˞ʹॻ͍ͯ͋Γ·͢
    ˞IUUQQIQOFUNBOVBMKBGVODUJPOTBSHVNFOUTQIQGVODUJPOTBSHVNFOUTUZQFEFDMBSBUJPO

    View full-size slide

  7. ܕએݴͷແ͍࣮૷

    View full-size slide

  8. class Calculator
    {
    public static function pow3($n)
    {
    return $n ** 3;
    }
    }
    print Calculator::pow3(3);
    // => 27

    View full-size slide

  9. class Calculator
    {
    public static function pow3($n)
    {
    return $n ** 3;
    }
    }
    print Calculator::pow3("string");
    // ???

    View full-size slide

  10. class Calculator
    {
    public static function pow3($n)
    {
    return $n ** 3;
    }
    }
    print Calculator::pow3("string");
    // => 0

    View full-size slide

  11. w จࣈྻΛೖྗͨ͠৔߹
    w ֘౰ͷϝιουͰ͸ΤϥʔʹͳΒͳ͍͕ɺ

    ͦ΋ͦ΋๬·͍͠ೖྗͰ͸ͳ͘ɺ݁Ռͷ͸ແҙຯ
    w ଓ͘ॲཧͰΤϥʔʹͳͬͨͱ͖ɺσόοά͕ࠔ೉ʹ
    w (BSCBHFJO HBSCBHFPVU
    w ແҙຯͳೖྗ͔Β͸ແҙຯͳ݁Ռ͕ಘΒΕΔ

    View full-size slide

  12. Ҿ਺ΛνΣοΫͯ͠ΈΔ

    View full-size slide

  13. class CalculateException extends Exception {}
    class Calculator
    {
    public static function pow3($n)
    {
    if (!is_int($n)) {
    throw new CalculateException;
    }
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3('string');
    } catch (CalculateException $e) {
    print 'Exception!!!';
    }
    // => 'Exception!!!'

    View full-size slide

  14. w Ҿ਺૿͑ͨͱ͖ʹՄಡੑ͕Լ͕Δ
    w &YDFQUJPO͸༧૝͞ΕΔΤϥʔ΁ͷରॲ๏Ͱ͋ͬͯɺ

    ͋ͬͯ͸ͳΒͳ͍ࣄ৅ ࠓճͷ৔߹ͩͱจࣈྻΛೖྗ͢Δ͜ͱ

    ͷରॲ๏Ͱ͸ͳ͍ͷͰ͸ʁ
    w ෆਖ਼ͳೖྗ͸ͦ΋ͦ΋ड͚෇͚ͳ͍Α͏ʹ͍ͨ͠

    View full-size slide

  15. ͦ͜Ͱܕએݴ

    View full-size slide

  16. class Calculator
    {
    public static function pow3(int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3(3);
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // => 27

    View full-size slide

  17. class Calculator
    {
    public static function pow3(int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3("string");
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // => "TypeError!"

    View full-size slide

  18. class Calculator
    {
    public static function pow3(int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3("3");
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // ???

    View full-size slide

  19. class Calculator
    {
    public static function pow3(int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3("3");
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // => 27

    View full-size slide

  20. declare(strict_types=1);
    class Calculator
    {
    public static function pow3(int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3("3");
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // => "TypeError!"
    SF

    View full-size slide

  21. class Calculator
    {
    public static function pow3(int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3(null);
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // ???

    View full-size slide

  22. class Calculator
    {
    public static function pow3(int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3(null);
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // => "TypeError!"

    View full-size slide

  23. class Calculator
    {
    public static function pow3(?int $n) : int
    {
    return $n ** 3;
    }
    }
    try {
    print Calculator::pow3(null);
    } catch (TypeError $e) {
    print "TypeError!";
    }
    // => 0
    OVMMBCMF

    View full-size slide

  24. w ܕએݴͰೖग़ྗΛ੍ݶ͢Δ
    w ؒҧͬͨ࢖͍ํ͕Ͱ͖ͳ͍Α͏ʹ͢Δ
    w ๷ޚతϓϩάϥϛϯάͷख๏
    w ࢖͍खʹաࣦ͕͋ͬͯ΋ඃ֐͕ٴ͹ͳ͍Α͏ʹ͢Δ
    w OPHBSCBHFBMMPXFEJO
    w ͱ͍͏Α͏ͳ͜ͱ͕$PEF$PNQMFUFʹॻ͔ΕͯΔ
    w ձࣾͷຊ୨ʹ͋Γ·͢

    View full-size slide

  25. IUUQTTQFBLFSEFDLDPNUXBEBQIQDPOGFSFODF
    ΑΓ࣮ફతͳ಺༰͸U@XBEB͞ΜͷεϥΠυΛಡΈ·͠ΐ͏

    View full-size slide

  26. ·ͱΊ
    w 1)1ʹ͸ܕએݴͱ͍͏ػೳ͕͋Δ
    w ܕએݴΛ࢖͏͜ͱͰϝιουͷҾ਺ɾฦΓ஋ͷܕΛࢦఆͰ͖Δ
    w ೖྗΛ੍ݶ͢Δ͜ͱͰ࢖͍ख͕ؒҧ͑Δ͜ͱΛࠔ೉ʹͰ͖Δ

    View full-size slide