2015-07-19 13 views
8

最近我正在檢查PHP 7,特別是return type declarationtype hinting。我已經從源代碼(Github的主分支)編譯PHP 7並在Ubuntu 14.04虛擬框中運行它。我試着運行下面的代碼來測試新的Exceptions。但它給了一個空白頁面。空白頁如果我聲明(strict_types = 1);在PHP 7頂部的文件

<?php 

function test(): string { 

    return []; 
} 

echo test(); 

然後我意識到我必須設置錯誤才能顯示在屏幕上。所以我加了老式ini_set('display_errors', 1);像下面,

<?php 
ini_set('display_errors', 1); 

function test(): string { 

    return []; 
} 

echo test(); 

這給了我以下TypeError根據本Throwable interface RFC

Fatal error: Uncaught TypeError: Return value of test() must be of the type string, array returned in /usr/share/nginx/html/test.php on line 7 in /usr/share/nginx/html/test.php:7 Stack trace: #0 /usr/share/nginx/html/test.php(10): test() #1 {main} thrown in /usr/share/nginx/html/test.php on line 7

進一步挖掘我在下面的上部加入declare(strict_types=1);不如預期,

<?php declare(strict_types=1); 

ini_set('display_errors', 1); 

function test(): string { 

    return []; 
} 

echo test(); 

和砰,錯誤剛剛消失,留下我的空白頁。我不知道爲什麼它給了我一個空白頁?

回答

15

圍繞谷歌的搜索和RFC的後,我來follwing句子RFC

This RFC further proposes the addition of a new optional per-file directive, declare(strict_types=1);, which makes all function calls and return statements within a file have 「strict」 type-checking for scalar type declarations, including for extension and built-in PHP functions.

這意味着有什麼不妥指令declare(strict_types=1)但問題是我打電話ini_set()功能的方式。它期望第二個參數是string類型。

string ini_set (string $varname , string $newvalue) 

我經過int代替,因此顯示的錯誤所需要的設置本身無法設置,因此我被擊中 與PHP嚴格模式一個空白頁。然後,我改變了一下代碼,並通過了字符串"1"如下,它的工作。

<?php declare(strict_types=1); 

ini_set('display_errors', "1"); 

function test(): string { 

    return []; 
} 

echo test(); 
+4

感謝您回來回答,我可能會遇到同樣的事情,當我終於開始玩7 – dops

0

作爲錯誤狀態你的函數期望你返回字符串,而是你返回一個數組!和功能抱怨這是正常的。所以你的回報只需要放一些字符串值。而已!