PHP 常量包含数组?
这失败了:
define('DEFAULT_ROLES', array('guy', 'development team'));
显然,常量不能保存数组。解决这个问题的最佳方法是什么?
define('DEFAULT_ROLES', 'guy|development team');
//...
$default = explode('|', DEFAULT_ROLES);
这似乎是不必要的努力。
This failed:
define('DEFAULT_ROLES', array('guy', 'development team'));
Apparently, constants can't hold arrays. What is the best way to get around this?
define('DEFAULT_ROLES', 'guy|development team');
//...
$default = explode('|', DEFAULT_ROLES);
This seems like unnecessary effort.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
从 PHP 5.6 开始,您可以使用
const
声明数组常量:短语法也可以工作,正如您所期望的:
如果您有 PHP 7,您最终可以使用
define(),就像您第一次尝试一样:
Since PHP 5.6, you can declare an array constant with
const
:The short syntax works too, as you'd expect:
If you have PHP 7, you can finally use
define()
, just as you had first tried:您还可以序列化数组,然后将其放入常量中:
You can also serialize your array and then put it into the constant:
您可以将它们存储为类的静态变量:
如果您不喜欢数组可以被其他人更改的想法,getter 可能会有所帮助:
编辑
从 PHP5.4 开始,甚至可以无需中间变量即可访问数组值,即以下工作:
You can store them as static variables of a class:
If you don't like the idea that the array can be changed by others, a getter might help:
EDIT
Since PHP5.4, it is even possible to access array values without the need for intermediate variables, i.e. the following works:
我正在这样使用它。我希望,它会帮助其他人。
config.php
在文件中,我需要常量。
I am using it like this. I hope, it will help others.
config.php
In file, where I need constants.
这就是我用的。它与 Soulmerge 提供的示例类似,但这样您可以获取完整数组或仅获取数组中的单个值。
像这样使用它:
This is what I use. It is similar to the example provided by soulmerge, but this way you can get the full array or just a single value in the array.
Use it like this:
PHP 7+
从 PHP 7 开始,您只需使用 define() 函数定义常量数组:
PHP 7+
As of PHP 7, you can just use the define() function to define a constant array :
您可以将其作为 JSON 字符串存储在常量中。从应用程序的角度来看,JSON 在其他情况下也很有用。
You can store it as a JSON string in a constant. And application point of view, JSON can be useful in other cases.
我知道这是一个有点老的问题,但这是我的解决方案:
我定义它是因为我需要将对象和数组存储在常量中,所以我还将 runkit 安装到 php,这样我就可以使 $const 变量成为超全局变量。
您可以将其用作
$const->define("my_constant",array("my","values"));
或只是$const->my_constant = array(" my","values");
要获取值,只需调用
$const->my_constant;
I know it's a bit old question, but here is my solution:
I defined it because I needed to store objects and arrays in constants so I installed also runkit to php so I could make the $const variable superglobal.
You can use it as
$const->define("my_constant",array("my","values"));
or just$const->my_constant = array("my","values");
To get the value just simply call
$const->my_constant;
是的,您可以将数组定义为常量。从PHP 5.6开始,可以将常量定义为标量表达式,也可以定义数组常量。可以将常量定义为资源,但应避免这样做,因为它可能会导致意外结果。
参考此链接
祝您编码愉快。
Yes, You can define an array as constant. From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.
With the reference of this link
Have a happy coding.
甚至可以使用关联数组......例如在类中。
Can even work with Associative Arrays.. for example in a class.
如果您使用 PHP 7 & 7+,你也可以像这样使用 fetch
if you're using PHP 7 & 7+, you can use fetch like this as well
使用爆炸和内爆函数,我们可以临时提出一个解决方案:
这将回显
电子邮件
。如果你希望它进一步优化,你可以定义 2 个函数来为你做重复的事情,如下所示:
希望有帮助。快乐编码。
Using explode and implode function we can improvise a solution :
This will echo
email
.If you want it to optimize it more you can define 2 functions to do the repetitive things for you like this :
Hope that helps . Happy coding .
执行某种 ser/deser 或编码/解码技巧看起来很丑陋,并且需要您记住当您尝试使用常量时到底做了什么。我认为带有访问器的类私有静态变量是一个不错的解决方案,但我会给你一个更好的解决方案。只需有一个公共静态 getter 方法即可返回常量数组的定义。这需要最少的额外代码,并且数组定义不会被意外修改。
如果你想让它看起来像一个定义的常量,你可以给它一个全部大写的名称,但是记住在名称后面添加“()”括号会很混乱。
我想您可以使该方法成为全局的,以更接近您所要求的 Define() 功能,但无论如何您确实应该限制常量名称并避免全局变量。
Doing some sort of ser/deser or encode/decode trick seems ugly and requires you to remember what exactly you did when you are trying to use the constant. I think the class private static variable with accessor is a decent solution, but I'll do you one better. Just have a public static getter method that returns the definition of the constant array. This requires a minimum of extra code and the array definition cannot be accidentally modified.
If you want to really make it look like a defined constant you could give it an all caps name, but then it would be confusing to remember to add the '()' parentheses after the name.
I suppose you could make the method global to be closer to the define() functionality you were asking for, but you really should scope the constant name anyhow and avoid globals.
你可以这样定义
You can define like this
常量只能包含标量值,我建议您存储数组的序列化(或 JSON 编码表示)。
Constants can only contain scalar values, I suggest you store the serialization (or JSON encoded representation) of the array.
如果您从 2009 年开始查看此内容,并且不喜欢 AbstractSingletonFactoryGenerators,那么这里还有一些其他选项。
请记住,数组在分配时会被“复制”,或者在本例中返回时会被“复制”,因此您实际上每次都会获得相同的数组。 (请参阅 PHP 中数组的写时复制行为。)
If you are looking this from 2009, and you don't like AbstractSingletonFactoryGenerators, here are a few other options.
Remember, arrays are "copied" when assigned, or in this case, returned, so you are practically getting the same array every time. (See copy-on-write behaviour of arrays in PHP.)
警告如果您使用 spl_autoload_register(..) 函数并使用数组定义常量,则类注册顺序按字母顺序排列。我没有得到包含数组的常量,因为我在 B 类中声明了它,并想在 A 类中使用它,我认为常量是全局的,无论它们在哪里声明,但这是错误的!初始化常量的文件的顺序很重要。
希望有所帮助。
Warning if you use the spl_autoload_register(..) function and you define constants with arrays, the class registration order is in alphabetical order. I didn't get my constant containing an array because I declared it in class B and wanted to use it in my class A, I thought constants were global no matter where they are declared, but it is wrong ! The order of the files in which the constants are initialized is important.
Hoping to have helped.