什么是最"雅致"方式来定义PHP中的全局常量数组 [英] What is the most "elegant" way to define a global constant array in PHP

查看:128
本文介绍了什么是最"雅致"方式来定义PHP中的全局常量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,你认为是定义一个常量数组变量类似的方式,最好的和干净的方式确定功能工作。我见过很多人要求在谷歌这个问题,到目前为止,我想出了最简单的解决方法是使用PHP的连载确定语句中的功能,像这样

 定义(MY_ARRAY连载(阵列(KEY1=> $值1键2=> $值2,..)));

然后使用定义的常量,你可以做这样的事情:

  $ MY_ARRAY =反序列化(MY_ARRAY)
的print_r($ MY_ARRAY);

不知道,如果你有很多的连载功能会让你放慢脚步的确定在code。你觉得呢?


解决方案

  $ GLOBALS ['MY_ARRAY'] =阵列();

I was wondering what do you think would be the best and cleanest way to define a constant array variable similar to the way define function works. I've seen a lot of people asking this question in Google and so far the easiest solution I have come up with is to use the PHP serialize function inside the define statement, like this

define ("MY_ARRAY", serialize (array ("key1" => $value1,"key2" => $value2, ..)));

then to use the defined constant you can do something like this:

$MY_ARRAY = unserialize (MY_ARRAY)
print_r ($MY_ARRAY);

Not sure if the serialize function will slow you down if you have a lot of defines in your code. What do you think?

解决方案

$GLOBALS['MY_ARRAY'] = array();

这篇关于什么是最"雅致"方式来定义PHP中的全局常量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆