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, &&, ?, ;, ||, &&, |, ^, &, ==, !=, ===, !==, <=,
>=, <, >, <<, >>, +, -, *, /, %, '.', ], (, ), :
----
  • 写回答

1条回答 默认 最新

  • duanjiu1894 2014-10-21 14:15
    关注

    Apparently if I use const outside of a class, the error in netbeans IDE won't show up.

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

    This should work!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥20 双系统开机引导中windows系统消失问题?