PHP - 使用数组作为类常量

Mar*_*tin 61 php arrays const

可能重复:
是否可以将数组声明为常量

是否可以在PHP中使用数组作为类常量?

const MYARRAY = array('123', '234');
Run Code Online (Sandbox Code Playgroud)

如果不是为什么?

xda*_*azz 74

不,你不能.

但是你可以将它声明为静态属性.

public static $MYARRAY = array('123', '234');
Run Code Online (Sandbox Code Playgroud)

---------------更新-----------------------------

数组const可从PHP 5.6获得.

php.net/manual/en/migration56.new-features.php

  • 这可能与常量不同,因为值可以更改 (8认同)
  • @Martin此外,如果你想要像公共常量这样的东西,你可以声明一个公共的getter. (2认同)

Alv*_*ong 65

更新:

现在可以在PHP 5.6中使用https://php.net/manual/en/migration56.new-features.php


不,你不能将数组分配给PHP常量.

http://www.php.net/manual/en/language.constants.syntax.php中

常量只能评估标量值

这就是原因.

对于实施例的标量值是int,float,string

  • 现在可以在PHP 5.6 http://php.net/manual/en/migration56.new-features.php中找到它 (41认同)