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

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

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

D8fb02b13755bda3ce991f198c42523a?s=128

Yoshihide TANIGUCHI

December 06, 2017
Tweet

Transcript

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

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

  3. w ൃදͷ಺༰Λ͖ͬͪΓ֮͑Δඞཁ͸ͳ͍Ͱ͢ w ͜ͷ৔ʹ͍Δਓ͕কདྷ1)1ͷίʔυΛॻ͘ͱ͖ʹ
 ʮͦ͏͍͑͹͍͋͋͏ػೳ͕͋ͬͨؾ͕͢Δʯͱ
 ࢥ͍ग़ͯ͠΋Β͑Ε͹े෼ w ػೳͷ࢖͍ํ͸ௐ΂ΒΕΔ͚Ͳ
 ػೳͷଘࡏ͸ೳಈతʹ௥ͬͯΔਓҎ֎ؾ͚ͮͳ͍

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

  5. class Calculator { public static function pow3(int $n) : int

    { return $n ** 3; } } try { print Calculator::pow3(3); } catch (TypeError $e) { print "TypeError!"; } // => 27
  6. ܕએݴ w ݹ͘͸λΠϓώϯςΟϯάͱ΋ w ϝιου΍ؔ਺ͷҾ਺ɾฦΓ஋ͷܕΛࢦఆ͢Δػೳ w 1)1͔Βଘࡏ͕ͨ͠ɺόʔδϣϯ্͕͕ΔʹͭΕɺ
 ΑΓଟ͘ͷܕ͕ࢦఆͰ͖ΔΑ͏ʹͳͬͨ w ֤όʔδϣϯͰࢦఆͰ͖Δܕʹ͍ͭͯ͸


    υΩϡϝϯτ˞ʹॻ͍ͯ͋Γ·͢ ˞IUUQQIQOFUNBOVBMKBGVODUJPOTBSHVNFOUTQIQGVODUJPOTBSHVNFOUTUZQFEFDMBSBUJPO
  7. ܕએݴͷແ͍࣮૷

  8. class Calculator { public static function pow3($n) { return $n

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

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

    ** 3; } } print Calculator::pow3("string"); // => 0
  11. w จࣈྻΛೖྗͨ͠৔߹ w ֘౰ͷϝιουͰ͸ΤϥʔʹͳΒͳ͍͕ɺ
 ͦ΋ͦ΋๬·͍͠ೖྗͰ͸ͳ͘ɺ݁Ռͷ͸ແҙຯ w ଓ͘ॲཧͰΤϥʔʹͳͬͨͱ͖ɺσόοά͕ࠔ೉ʹ w (BSCBHFJO HBSCBHFPVU

    w ແҙຯͳೖྗ͔Β͸ແҙຯͳ݁Ռ͕ಘΒΕΔ
  12. Ҿ਺ΛνΣοΫͯ͠ΈΔ

  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!!!'
  14. w Ҿ਺૿͑ͨͱ͖ʹՄಡੑ͕Լ͕Δ w &YDFQUJPO͸༧૝͞ΕΔΤϥʔ΁ͷରॲ๏Ͱ͋ͬͯɺ
 ͋ͬͯ͸ͳΒͳ͍ࣄ৅ ࠓճͷ৔߹ͩͱจࣈྻΛೖྗ͢Δ͜ͱ 
 ͷରॲ๏Ͱ͸ͳ͍ͷͰ͸ʁ w ෆਖ਼ͳೖྗ͸ͦ΋ͦ΋ड͚෇͚ͳ͍Α͏ʹ͍ͨ͠

  15. ͦ͜Ͱܕએݴ

  16. class Calculator { public static function pow3(int $n) : int

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

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

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

    { return $n ** 3; } } try { print Calculator::pow3("3"); } catch (TypeError $e) { print "TypeError!"; } // => 27
  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 ☺
  21. class Calculator { public static function pow3(int $n) : int

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

    { return $n ** 3; } } try { print Calculator::pow3(null); } catch (TypeError $e) { print "TypeError!"; } // => "TypeError!"
  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
  24. w ܕએݴͰೖग़ྗΛ੍ݶ͢Δ w ؒҧͬͨ࢖͍ํ͕Ͱ͖ͳ͍Α͏ʹ͢Δ w ๷ޚతϓϩάϥϛϯάͷख๏ w ࢖͍खʹաࣦ͕͋ͬͯ΋ඃ֐͕ٴ͹ͳ͍Α͏ʹ͢Δ w OPHBSCBHFBMMPXFEJO

    w ͱ͍͏Α͏ͳ͜ͱ͕$PEF$PNQMFUFʹॻ͔ΕͯΔ w ձࣾͷຊ୨ʹ͋Γ·͢
  25. IUUQTTQFBLFSEFDLDPNUXBEBQIQDPOGFSFODF ΑΓ࣮ફతͳ಺༰͸U@XBEB͞ΜͷεϥΠυΛಡΈ·͠ΐ͏

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