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

Usage of PHP Type Hinting

Usage of PHP Type Hinting

登壇:
言語学習のためのミニ勉強会 at 株式会社ベーシック

Kota Hashihama

July 21, 2020
Tweet

More Decks by Kota Hashihama

Other Decks in Programming

Transcript

  1. function calculateSpeed(int $distance, int $time) { return $distance / $time;

    /* ex) 12.3 */ } With Argument function isAdminLoggedIn(): bool { return currentUser()->role === ‘admin’; /* ex) true */ } With Returned Value function sayHello(): void { echo ‘Hello’; } // returns nothing class { private Foo $foo; } With Property
  2. function add(int $a, int $b): int { return $a +

    $b; } add(1.0, 2.0); // 3 Strict Type Union Type <?php declare(strict_types=1); function add(int $a, int $b): int { return $a + $b; } add(1.0, 2.0); // TypeError! float given ?string int|float iterable string or null int or float array or Traversable 8.0 avoids type conversion accepts multiple types