Slide 1

Slide 1 text

PHP7 Scalar type declarations

Slide 2

Slide 2 text

自己紹介 ほたて @hotatekun 好きなもの - ほたて(禁ホタテ中) - からあげ

Slide 3

Slide 3 text

PHP7のタイプヒンティング

Slide 4

Slide 4 text

タイプヒンティング?

Slide 5

Slide 5 text

タイプヒンティング? こんなの public function return_i(int $i) { return $i; } これ

Slide 6

Slide 6 text

いままで書けなかったのか?

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

歴史 5.0 クラス名 インターフェース名 self 5.1 array 5.4 callable 7.0 (new!) bool float int string alias(booleanやinteger)は書けないので注意

Slide 9

Slide 9 text

example

Slide 10

Slide 10 text

タイプヒンティング public function method1(int $i, string $s) { // hogehoge } こんな感じで書ける

Slide 11

Slide 11 text

タイプヒンティング public function return_i(int $i): int { return $i; } 戻り値もかける

Slide 12

Slide 12 text

タイプヒンティング public function getClosure() { return function(string $s): string { return $s; }; } クロージャにもかける

Slide 13

Slide 13 text

エラー処理

Slide 14

Slide 14 text

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()); }

Slide 15

Slide 15 text

recoverable fatal error Catchable fatal error: Argument 1 passed to Test:: method1() must be of the type array, integer given, called in...

Slide 16

Slide 16 text

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()); }

Slide 17

Slide 17 text

Catchできる(TypeErrorが発生) string(9) "TypeError" string(127) "Argument 1 passed to Test::method1() must be of the type array, integer given, called in

Slide 18

Slide 18 text

強い型付け

Slide 19

Slide 19 text

厳密な型チェック

Slide 20

Slide 20 text

弱い型チェック

Slide 21

Slide 21 text

変換例(PHP5)

Slide 22

Slide 22 text

変換例(PHP7 厳密な型チェック)

Slide 23

Slide 23 text

変換例(PHP7 弱い型チェック)

Slide 24

Slide 24 text

変換例(PHP7 弱い型チェック) ----------------------------------------------------------------- | (boolean) (float) (integer) (string) ----------------------------------------------------------------- boolean | - float integer string float | boolean - integer string integer | boolean float - string string | boolean TypeError TypeError - -----------------------------------------------------------------

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

future(PHP7.1?)

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Resource typehint function only_accepts_resource(resource $foo) { } only_accepts_resource(fopen('php://memory', 'w')); https://wiki.php.net/rfc/resource_typehint

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

まとめ PHP7.0から新しいScalar型の宣言が導入された。 strict_type=1はファイル単位。 使うときは、厳密な型チェック、弱い型チェック、従来の 動きの3パターンの考慮が必要。 PHP7.1以降で新たな型宣言が生まれる可能性。