PHP 7 - Declaraciones de tipo de retorno

En PHP 7, una nueva característica, Return type declarationsha sido introducido. La declaración de tipo de retorno especifica el tipo de valor que debe devolver una función. Se pueden declarar los siguientes tipos para los tipos de devolución.

  • int
  • float
  • bool
  • string
  • interfaces
  • array
  • callable

Ejemplo: tipo de devolución válido

<?php
   declare(strict_types = 1);
   function returnIntValue(int $value): int {
      return $value;
   }
   print(returnIntValue(5));
?>

Produce la siguiente salida del navegador:

5

Ejemplo: tipo de devolución no válido

<?php
   declare(strict_types = 1);
   function returnIntValue(int $value): int {
      return $value + 1.0;
   }
   print(returnIntValue(5));
?>

Produce la siguiente salida del navegador:

Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned...