Nic*_*ner 387 php arrays scalar constants
这失败了:
define('DEFAULT_ROLES', array('guy', 'development team'));
Run Code Online (Sandbox Code Playgroud)
显然,常量不能保存数组.解决这个问题的最佳方法是什么?
define('DEFAULT_ROLES', 'guy|development team');
//...
$default = explode('|', DEFAULT_ROLES);
Run Code Online (Sandbox Code Playgroud)
这似乎是不必要的努力.
And*_*rea 789
从PHP 5.6开始,您可以声明一个数组常量const:
<?php
const DEFAULT_ROLES = array('guy', 'development team');
Run Code Online (Sandbox Code Playgroud)
正如您所期望的那样,短语法也有效:
<?php
const DEFAULT_ROLES = ['guy', 'development team'];
Run Code Online (Sandbox Code Playgroud)
如果你有PHP 7,你可以最终使用define(),就像你第一次尝试一样:
<?php
define('DEFAULT_ROLES', array('guy', 'development team'));
Run Code Online (Sandbox Code Playgroud)
小智 488
注意:虽然这是公认的答案,但值得注意的是,在PHP 5.6+中你可以拥有const数组 - 请参阅下面的Andrea Faulds的回答.
您还可以序列化您的数组,然后将其放入常量:
# define constant, serialize array
define ("FRUITS", serialize (array ("apple", "cherry", "banana")));
# use it
$my_fruits = unserialize (FRUITS);
Run Code Online (Sandbox Code Playgroud)
sou*_*rge 141
您可以将它们存储为类的静态变量:
class Constants {
public static $array = array('guy', 'development team');
}
# Warning: array can be changed lateron, so this is not a real constant value:
Constants::$array[] = 'newValue';
Run Code Online (Sandbox Code Playgroud)
如果您不喜欢其他人可以更改阵列的想法,那么getter可能有所帮助:
class Constants {
private static $array = array('guy', 'development team');
public static function getArray() {
return self::$array;
}
}
$constantArray = Constants::getArray();
Run Code Online (Sandbox Code Playgroud)
编辑
从PHP5.4开始,甚至可以访问数组值而无需中间变量,即以下工作:
$x = Constants::getArray()['index'];
Run Code Online (Sandbox Code Playgroud)
Jas*_*ant 40
我这样用它.我希望,它会帮助别人.
config.php文件
class app{
private static $options = array(
'app_id' => 'hello',
);
public static function config($key){
return self::$options[$key];
}
}
Run Code Online (Sandbox Code Playgroud)
在文件中,我需要常量.
require('config.php');
print_r(app::config('app_id'));
Run Code Online (Sandbox Code Playgroud)
Syc*_*one 12
这就是我使用的.它类似于soulmerge提供的示例,但这样您就可以获得完整数组或数组中的单个值.
class Constants {
private static $array = array(0 => 'apple', 1 => 'orange');
public static function getArray($index = false) {
return $index !== false ? self::$array[$index] : self::$array;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
Constants::getArray(); // Full array
// OR
Constants::getArray(1); // Value of 1 which is 'orange'
Run Code Online (Sandbox Code Playgroud)
小智 9
您可以将它作为JSON字符串存储在常量中.从应用的角度来看,JSON在其他情况下可能很有用.
define ("FRUITS", json_encode(array ("apple", "cherry", "banana")));
$fruits = json_decode (FRUITS);
var_dump($fruits);
Run Code Online (Sandbox Code Playgroud)
从PHP 5.6开始,您可以使用const下面的关键字定义常量数组
const DEFAULT_ROLES = ['test', 'development', 'team'];
Run Code Online (Sandbox Code Playgroud)
和不同的元素可以访问如下:
echo DEFAULT_ROLES[1];
....
Run Code Online (Sandbox Code Playgroud)
从PHP 7开始,可以使用define如下定义常量数组:
define('DEFAULT_ROLES', [
'test',
'development',
'team'
]);
Run Code Online (Sandbox Code Playgroud)
并且可以像以前一样访问不同的元素.
从 PHP 7 开始,您可以只使用define()函数来定义一个常量数组:
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // outputs "cat"
Run Code Online (Sandbox Code Playgroud)