PHP - 使用数组作为类常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11184469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:56:06  来源:igfitidea点击:

PHP - use array as class constant

phparraysconst

提问by Martin

Possible Duplicate:
Is it possible to declare an array as constant

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

Is it possible to use an array as a class constant in PHP?

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

I.e

IE

const MYARRAY = array('123', '234');

If not why?

如果不是为什么?

回答by xdazz

No, you can't.

不,你不能。

But you could declare it as a static property.

但是您可以将其声明为静态属性。

public static $MYARRAY = array('123', '234');

---------------Update-----------------------------

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

Array const is available from PHP 5.6.

数组常量从 PHP 5.6 开始可用。

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

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

回答by Alvin Wong

UPDATE:

更新:

This is now available in PHP 5.6 https://php.net/manual/en/migration56.new-features.php

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



No you can't assign an Array to PHP constant.

不,您不能将数组分配给 PHP 常量。

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

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

Constants may only evaluate to scalar values

常量只能计算为标量值

This is the reason.

这就是原因。

Scalar values for examples are int, float, string

示例的标量值为int, float,string