dpglo66848
2014-10-20 18:31
浏览 244
已采纳

Const数组在php 5.6中运行?

In the php manual, it is stated in user contributed notes that const array is now allowed. I even checked other posts here in stackoverflow and they said the same. I tested the following code:

<?php

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

Output:

ball
car
apple

The above code works. However This doesn't work if I use define('test',array("a"=>"apple","b"=>"ball","c"=>"car") outside of the class. Is this a new undocumented change or happening only in my setup?

My setup is php 5.6.1(32-bit thread safe) and apache 2.4.10 (32-bit) on windows 7 x64. I downloaded them from the their respective sites directly. I didn't use any WAMP stacks.

Also note that I am using netbeans 8.0.1 IDE for hints and it shows this error. Anyone know how to remove it?

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

图片转代码服务由CSDN问答提供 功能建议

在php manual ,在用户提供的注释中声明现在允许使用const数组。 我甚至在stackoverflow中检查了其他帖子,他们也说了同样的话。 我测试了以下代码:</ p>

 &lt;?php 
 
class Constants {
 const test = array(“a”=&gt;“apple”,“b”  =&gt;“ball”,“c”=&gt;“car”); 
} 
echo Constants :: test [“b”]; 
echo Constants :: test [“c”]; 
echo Constants :: 试验[ “一个”]; 
&GT; 
 </代码> </ PRE> 
 
 

输出:</ p>

 <代码>球\ NCAR 
apple  
 </ code> </ pre> 
 
 

以上代码有效。 但是,如果我使用 define('test',array(“a”=&gt;“apple”,“b”=&gt;“ball”,“c”=&gt;“car”),这不起作用 </ code>在类之外。这是一个新的未记录的更改还是仅在我的设置中发生?</ p>

我的设置是php 5.6.1(32位线程安全)和apache 2.4 Windows 7 x64上的.10(32位)。我直接从各自的站点下载它们。我没有使用任何WAMP堆栈。</ p>

另请注意我使用的是netbeans 8.0 .1用于提示的IDE,它显示此错误。有人知道如何删除它吗?</ p>

 语法错误
unexpected:[
after:identifier'test'
expected:instanceof,  as,=&gt;,},',',OR,XOR,&amp;&amp;,?,;,||,&amp;&amp;,|,^,&amp;,==,!=,===,  !==,&lt; =,
&gt; =,&lt;,&gt;,&lt;&lt;,&gt;&gt;,+, - ,*,/,%,'。',],(,),:  
 ---- 
 </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题