【发布时间】:2015-10-08 16:10:25
【问题描述】:
最近我正在检查 PHP 7,特别是 return type declaration 和 type hinting。我已经从源代码编译了 PHP 7(来自Github 的主分支)并在 Ubuntu 14.04 虚拟框中运行它。我尝试运行以下代码来测试新的Exceptions。但它给出了一个空白页。
<?php
function test(): string {
return [];
}
echo test();
然后我意识到我必须设置要在屏幕上显示的错误。所以我添加了老式的ini_set('display_errors', 1);,如下所示,
<?php
ini_set('display_errors', 1);
function test(): string {
return [];
}
echo test();
这让我按照Throwable interface RFC 的预期关注了TypeError
致命错误:未捕获的类型错误:test() 的返回值必须是 类型字符串,返回数组/usr/share/nginx/html/test.php 在线 7 在 /usr/share/nginx/html/test.php:7 堆栈跟踪:#0 /usr/share/nginx/html/test.php(10): test() #1 {main} 抛出 /usr/share/nginx/html/test.php 在第 7 行
进一步挖掘,我在顶部添加了declare(strict_types=1);,如下所示,
<?php declare(strict_types=1);
ini_set('display_errors', 1);
function test(): string {
return [];
}
echo test();
然后砰,错误消失了,给我留下了空白页。我不知道为什么它给了我一个空白页?
【问题讨论】: