$30 off During Our Annual Pro Sale. View Details »

PHP7.2で始める型宣言

 PHP7.2で始める型宣言

Talked in EC Tech Meeting in GMO Pepabo inc

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