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!

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

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴