2014-10-20 92 views
3

在php manual中,在用戶貢獻的註釋中指出,現在允許使用const數組。我甚至在stackoverflow中檢查了其他帖子,他們也這麼說。我測試以下代碼:常量數組在PHP 5.6中工作?

<?php 

class Constants{ 
     const test = array("a"=>"apple","b"=>"ball","c"=>"car"); 
} 
echo Constants::test["b"]; 
echo Constants::test["c"]; 
echo Constants::test["a"]; 
?> 

輸出:

ball 
car 
apple 

上述代碼工作。但是,如果我在班級以外使用define('test',array("a"=>"apple","b"=>"ball","c"=>"car"),這不起作用。這是一個新的沒有記錄的變化或僅在我的設置中發生?

我的設置是在Windows 7 x64上的php 5.6.1(32位線程安全)和apache 2.4.10(32位)。我直接從他們各自的站點下載它們。我沒有使用任何WAMP堆棧。

另請注意,我正在使用netbeans 8.0.1 IDE進行提示,並顯示此錯誤。任何人都知道如何刪除它?

Syntax error 
unexpected: [ 
after: identifier 'test' 
expected: instanceof, as, =>, }, ',', OR, XOR, &&, ?, ;, ||, &&, |, ^, &, ==, !=, ===, !==, <=, 
>=, <, >, <<, >>, +, -, *, /, %, '.', ], (,), : 
---- 
+0

從http://php.net/const:常量可能只能評估爲標量值或PHP 5.6及更高版本中的標量或數組值。 – 2014-10-20 18:45:27

+1

@RocketHazmat,但他有5.6 – Cheery 2014-10-20 18:46:12

+0

@Cheery:我想我錯讀了這個問題。 – 2014-10-20 18:47:26

回答

0

顯然,如果我在類的外部使用const,NetBeans IDE中的錯誤將不會顯示出來。

const test = array("a"=>"apple","b"=>"ball","c"=>"car"); 
echo test["b"]; 
echo test["c"]; 
echo test["a"]; 

這應該工作!