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. class Calculator { public static function pow3(int $n) : int

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

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

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

    ** 3; } } print Calculator::pow3("string"); // => 0
  5. 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!!!'
  6. class Calculator { public static function pow3(int $n) : int

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

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

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

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

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

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

    { return $n ** 3; } } try { print Calculator::pow3(null); } catch (TypeError $e) { print "TypeError!"; } // => 0 OVMMBCMF