Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP7 - Scalar Type Hints & Return Types

2,671 views

Published on

Type hints were introduced with PHP 5. PHP 7 will add the ability to type hint on scalars and even to explicitly state what type will be returned from the function. This talk briefly goes over these added features to PHP 7.

Published in: Software
  • Be the first to comment

PHP7 - Scalar Type Hints & Return Types

  1. 1. PHP7: Scalar Type Hints & Return Types 2015 April 1 Kansas City PHP User Group
  2. 2. PHP 7 It’s coming! Q4 2015 Image by Aaron Van Noy https://plus.google.com/+AaronVanNoy/posts/HPtSxAGcpAd
  3. 3. PHP 5 Type Hinting PHP 5.1 • Objects • Interfaces • Array PHP 5.4 • Callable
  4. 4. 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 %sn", getDayOfWeek($time));
 } 

  5. 5. 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 %sn", 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
  6. 6. 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 %sn", getDayOfWeek($time));
 }

  7. 7. 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 %sn", getDayOfWeek($time));
 }
 Today is Thursday Today is Sunday
  8. 8. PHP 7 Scalar Type Hinting PHP 5 Type Hinting +++ Scalars • Strings • Integers • Floats • Booleans
  9. 9. 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
  10. 10. PHP7 Scalar Type Hinting Example 1: Turned Off <?php
 declare(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;
  11. 11. PHP7 Scalar Type Hinting Example 1: Turned Off <?php
 declare(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
  12. 12. PHP7 Scalar Type Hinting Example 1: Turned On <?php
 declare(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;
  13. 13. PHP7 Scalar Type Hinting Example 1: Turned On <?php
 declare(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
  14. 14. 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
  15. 15. PHP 7 Return Types Example 1: DateTime <?php
 declare(strict_types=1);
 
 /**
 * @return DateTime
 */
 function getCurrentTime() : DateTime {
 return new DateTime('now');
 }
 
 echo getCurrentTime()->format('l') . PHP_EOL;

  16. 16. PHP 7 Return Types Example 1: DateTime <?php
 declare(strict_types=1);
 
 /**
 * @return DateTime
 */
 function getCurrentTime() : DateTime {
 return new DateTime('now');
 }
 
 echo getCurrentTime()->format('l') . PHP_EOL;
 Thursday
  17. 17. PHP 7 Return Types Example 1: Calculator <?php
 declare(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;

  18. 18. PHP 7 Return Types Example 1: Calculator <?php
 declare(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
  19. 19. PHP 7 Return Types Example 1: Calculator <?php
 declare(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;

  20. 20. PHP 7 Return Types Example 1: Calculator <?php
 declare(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
  21. 21. 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
  22. 22. 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/
  23. 23. Thank You Eric Poe
 eric@ericpoe.com
 @eric_poe Please rate this talk:
 https://joind.in/14348

×