PHP 7返回类型声明 – 致命错误

前端之家收集整理的这篇文章主要介绍了PHP 7返回类型声明 – 致命错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想尝试 PHP 7的返回类型声明(我在Windows上使用PHP 7RC3用于此目的).

我想从一件非常简单的事情开始:

  1. function getme() : integer
  2. {
  3. return 434;
  4. }
  5. echo getme();

但这给了我一个致命的错误

Fatal error: Uncaught TypeError: Return value of getme() must be an instance of integer,integer returned

然后我也尝试转换返回值,但是
return(整数)434;或return(int)434;给了我同样的错误;

最后我也尝试过:

  1. function getme() : integer
  2. {
  3. $i = 434;
  4. return (integer) $i;
  5. }
  6. echo getme();

结果相同.

我究竟做错了什么?
或者我在这里误解了什么?

感谢您的任何解释和帮助!

UPDATE
这就是为什么我认为我必须使用整数而不是int(特别注意Toby Allen):

https://wiki.php.net/rfc/return_types

Examples of Invalid Use
(…)

  1. // Int is not a valid type declaration
  2.  
  3. function answer(): int {
  4. return 42;
  5. }
  6. answer();

No new reserved words are added. The names int,float,string and
bool are recognised and allowed as type declarations,and prohibited
from use as class/interface/trait names (including with use and
class_alias).

来自:https://wiki.php.net/rfc/scalar_type_hints_v5

TL / DR:

  1. function getme() : int
  2. {
  3. return 434;
  4. }
  5.  
  6. echo getme();

猜你在找的PHP相关文章