php7 - scalar type hints & return types
TRANSCRIPT
PHP 7
It’s coming!
Q4 2015
Image by Aaron Van Noy https://plus.google.com/+AaronVanNoy/posts/HPtSxAGcpAd
PHP5 Type Hinting Example<?php/** * Type Hinting in PHP 5 is only for classes, * interfaces, & callable * * @param DateTime $timestamp * @return string */function getDayOfWeek(\DateTime $timestamp) { return $timestamp->format('l'); } $times = []; $times[] = new \DateTime('now'); $times[] = new \DateTimeImmutable('+3 days'); foreach($times as $time) { printf("Today is %s\n", getDayOfWeek($time)); }
PHP5 Type Hinting Example<?php/** * Type Hinting in PHP 5 is only for classes, * interfaces, & callable * * @param DateTime $timestamp * @return string */function getDayOfWeek(\DateTime $timestamp) { return $timestamp->format('l'); } $times = []; $times[] = new \DateTime('now'); $times[] = new \DateTimeImmutable('+3 days'); foreach($times as $time) { printf("Today is %s\n", getDayOfWeek($time)); }
Today is Thursday
Fatal error: Argument 1 passed to getDayOfWeek() must be an instance of DateTime, instance of DateTimeImmutable given, called in /vagrant_data/php5TypeHint.php on line 19 and defined in /vagrant_data/php5TypeHint.php on line 9
PHP5 Type Hinting Example<?php/** * Type Hinting in PHP 5 is only for classes, * interfaces, & callable * * @param DateTimeInterface $timestamp * @return string */function getDayOfWeek(\DateTimeInterface $timestamp) { return $timestamp->format('l'); } $times = []; $times[] = new \DateTime('now'); $times[] = new \DateTimeImmutable('+3 days'); foreach($times as $time) { printf("Today is %s\n", getDayOfWeek($time)); }
PHP5 Type Hinting Example<?php/** * Type Hinting in PHP 5 is only for classes, * interfaces, & callable * * @param DateTimeInterface $timestamp * @return string */function getDayOfWeek(\DateTimeInterface $timestamp) { return $timestamp->format('l'); } $times = []; $times[] = new \DateTime('now'); $times[] = new \DateTimeImmutable('+3 days'); foreach($times as $time) { printf("Today is %s\n", getDayOfWeek($time)); }
Today is Thursday Today is Sunday
Scalar Type Hinting
• Not turned on by default
• Turn on by making `declare(strict_types=1);` the first statement in your file
• Only strict on the file with the function call
PHP7 Scalar Type Hinting Example 1: Turned Off
<?phpdeclare(strict_types=0); /** * @param int $number * @param string $street * @param string $apt * @return string */function createStreetAddress(int $number, string $street, string $apt = null) { if ($apt) { return sprintf('%d %s, #%s', $number, $street, $apt); } else { return sprintf('%d %s', $number, $street); } } echo createStreetAddress(221, "Baker St", "B") . PHP_EOL; echo createStreetAddress("221", "Baker St", "B") . PHP_EOL;
PHP7 Scalar Type Hinting Example 1: Turned Off
<?phpdeclare(strict_types=0); /** * @param int $number * @param string $street * @param string $apt * @return string */function createStreetAddress(int $number, string $street, string $apt = null) { if ($apt) { return sprintf('%d %s, #%s', $number, $street, $apt); } else { return sprintf('%d %s', $number, $street); } } echo createStreetAddress(221, "Baker St", "B") . PHP_EOL; echo createStreetAddress("221", "Baker St", "B") . PHP_EOL;
221 Baker St, #B 221 Baker St, #B
PHP7 Scalar Type Hinting Example 1: Turned On
<?phpdeclare(strict_types=1); /** * @param int $number * @param string $street * @param string $apt * @return string */function createStreetAddress(int $number, string $street, string $apt = null) { if ($apt) { return sprintf('%d %s, #%s', $number, $street, $apt); } else { return sprintf('%d %s', $number, $street); } } echo createStreetAddress(221, "Baker St", "B") . PHP_EOL; echo createStreetAddress("221", "Baker St", "B") . PHP_EOL;
PHP7 Scalar Type Hinting Example 1: Turned On
<?phpdeclare(strict_types=1); /** * @param int $number * @param string $street * @param string $apt * @return string */function createStreetAddress(int $number, string $street, string $apt = null) { if ($apt) { return sprintf('%d %s, #%s', $number, $street, $apt); } else { return sprintf('%d %s', $number, $street); } } echo createStreetAddress(221, "Baker St", "B") . PHP_EOL; echo createStreetAddress("221", "Baker St", "B") . PHP_EOL;
221 Baker St, #B
Fatal error: Argument 1 passed to createStreetAddress() must be of the type integer, string given, called in /vagrant_data/php7TypeHint.php on line 20 and defined in /vagrant_data/php7TypeHint.php on line 10
Introducing… return types
• Completely optional
• Declare strict same as for Scalar Type Hinting
• Only strict on the file with the function declaration
• Tells the compiler that we expect to get something of type Foo out of the function call
PHP 7 Return Types Example 1: DateTime
<?phpdeclare(strict_types=1); /** * @return \DateTime */function getCurrentTime() : \DateTime { return new \DateTime('now'); } echo getCurrentTime()->format('l') . PHP_EOL;
PHP 7 Return Types Example 1: DateTime
<?phpdeclare(strict_types=1); /** * @return \DateTime */function getCurrentTime() : \DateTime { return new \DateTime('now'); } echo getCurrentTime()->format('l') . PHP_EOL;
Thursday
PHP 7 Return Types Example 1: Calculator
<?phpdeclare(strict_types=1); /** * @param int $num * @param int $denom * @return int */function divide(int $num, int $denom) : int { if (0 === $denom) { return 9999999999; } else { return $num / $denom; } } echo divide(7, 3) . PHP_EOL;
PHP 7 Return Types Example 1: Calculator
<?phpdeclare(strict_types=1); /** * @param int $num * @param int $denom * @return int */function divide(int $num, int $denom) : int { if (0 === $denom) { return 9999999999; } else { return $num / $denom; } } echo divide(7, 3) . PHP_EOL;
Fatal error: Return value of divide() must be of the type integer, float returned in /vagrant_data/php7ReturnType.php on line 13 in /vagrant_data/php7ReturnType.php on line 13
PHP 7 Return Types Example 1: Calculator
<?phpdeclare(strict_types=1); /** * @param int $num * @param int $denom * @return float */function divide(int $num, int $denom) : float { if (0 === $denom) { return 9999999999; } else { return $num / $denom; } } echo divide(7, 3) . PHP_EOL;
PHP 7 Return Types Example 1: Calculator
<?phpdeclare(strict_types=1); /** * @param int $num * @param int $denom * @return float */function divide(int $num, int $denom) : float { if (0 === $denom) { return 9999999999; } else { return $num / $denom; } } echo divide(7, 3) . PHP_EOL;
2.3333333333333
Great. PHP Just Got Hard. Again.
• No. These are optional
• Weak Typing is still the default
• Strict typing forces the programmer to think more clearly about a function’s input/output
• Think: “Filter input; escape output.”
• Leads the way to compiling PHP to Opcode
• Compiler can catch certain bugs before a user will
More Information
• Scalar Type Hinting & Return Types - RFC: https://wiki.php.net/rfc/scalar_type_hints_v5
• Anthony Ferrara’s blog post about this:http://blog.ircmaxell.com/2015/02/scalar-types-and-php.html
• Building & Testing PHP 7: http://akrabat.com/building-and-testing-php7/