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

PHP7 Scalar type declarations

gtakat
June 21, 2016

PHP7 Scalar type declarations

PHP7で導入された型宣言(タイプヒンティング)について

gtakat

June 21, 2016
Tweet

Other Decks in Technology

Transcript

  1. 歴史 5.0 クラス名 インターフェース名 self 5.1 array 5.4 callable 7.0

    (new!) bool float int string alias(booleanやinteger)は書けないので注意
  2. PHP5 class Test { public function method1(array $arr) { //

    ... } } $t = new Test(); try { $t->method1(10); } catch (Exception $e) { var_dump(get_class($e)); var_dump($e->getMessage()); }
  3. recoverable fatal error Catchable fatal error: Argument 1 passed to

    Test:: method1() must be of the type array, integer given, called in...
  4. PHP7では class Test { public function method1(array $arr) { //

    ... } } $t = new Test(); try { $t->method1(10); } catch (Throwable $e) { var_dump(get_class($e)); var_dump($e->getMessage()); }
  5. 厳密な型チェック <?php declare(strict_types=1); function test(int $i) { // ... }

    ファイルの先頭に strict_types=1を宣言する この場合int以外を渡すとエラー
  6. 変換例(PHP5) <?php function print_type($i) { var_dump("{$i} = " . gettype($i));

    } print_type(1); // integer print_type(1.5); // double print_type('2'); // string
  7. 変換例(PHP7 厳密な型チェック) <?php declare(strict_types=1); function print_type(int $i) { var_dump("{$i} =

    " . gettype($i)); } print_type(1); // integer print_type(1.5); // Fatal error print_type('2'); // Fatal error
  8. 変換例(PHP7 弱い型チェック) <?php function print_type(int $i) { var_dump("{$i} = "

    . gettype($i)); } print_type(1); // integer print_type(1.5); // integer print_type('2'); // integer
  9. 変換例(PHP7 弱い型チェック) ----------------------------------------------------------------- | (boolean) (float) (integer) (string) ----------------------------------------------------------------- boolean

    | - float integer string float | boolean - integer string integer | boolean float - string string | boolean TypeError TypeError - -----------------------------------------------------------------
  10. 変換例(PHP7 厳密な型チェック) ----------------------------------------------------------------- | (boolean) (float) (integer) (string) ----------------------------------------------------------------- boolean

    | - TypeError TypeError TypeError float | TypeError - TypeError TypeError integer | TypeError float - TypeError string | TypeError TypeError TypeError - -----------------------------------------------------------------
  11. Nullable Types function answer(): ?int { return null; //ok }

    function answer(): ?int { return 42; // ok } function answer(): ?int { return new stdclass(); // error } https://wiki.php.net/rfc/nullable_types
  12. Union Types function print_each(array | string $in) { foreach ((array)

    $in as $value) { echo $value, PHP_EOL; } } print_each(['Bob', 'Joe', 'Levi']); // ok print_each('Levi'); // ok print_each(new stdclass()); // TypeError https://wiki.php.net/rfc/union_types
  13. Void Return Type function lacks_return(): void { // valid }

    function returns_nothing(): void { return; // valid } function returns_one(): void { return 1; // Fatal error: A void function must not return a value } function returns_null(): void { return null; // Fatal error: A void function must not return a value } https://wiki.php.net/rfc/void_return_type
  14. Reserve More Types in PHP 7 https://wiki.php.net/rfc/reserve_more_types_in_php_7 reserve the aliases

    of these types, such as integer, double and boolean add explicit scalar type support for parameter and return types add union types, such as int|false reserve other types such as numeric, mixed or scalar