这个失败了:
define('DEFAULT_ROLES', array('guy', 'development team'));
显然,常量不能存储数组。如何最好地解决这个问题?
define('DEFAULT_ROLES', 'guy|development team');
//...
$default = explode('|', DEFAULT_ROLES);
这似乎是不必要的努力。
这个失败了:
define('DEFAULT_ROLES', array('guy', 'development team'));
显然,常量不能存储数组。如何最好地解决这个问题?
define('DEFAULT_ROLES', 'guy|development team');
//...
$default = explode('|', DEFAULT_ROLES);
这似乎是不必要的努力。
自 PHP 5.6 起,您可以使用 const
声明一个数组常量:
<?php
const DEFAULT_ROLES = array('guy', 'development team');
短语法也可以正常工作,如你所预期:
<?php
const DEFAULT_ROLES = ['guy', 'development team'];
如果您使用的是 PHP 7,您终于可以像最初尝试时那样使用 define()
:
<?php
define('DEFAULT_ROLES', array('guy', 'development team'));
PHP 5.6+ 引入了
const
数组 - 请参见 Andrea Faulds 的答案。
您还可以将数组序列化,然后将其放入常量中:
# define constant, serialize array
define ("FRUITS", serialize (array ("apple", "cherry", "banana")));
# use it
$my_fruits = unserialize (FRUITS);
const fruits = ['apple','cherry','banana'];
- Alex K您可以将它们存储为类的静态变量:
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';
如果您不喜欢数组会被他人更改的想法,可以使用getter方法:class Constants {
private static $array = array('guy', 'development team');
public static function getArray() {
return self::$array;
}
}
$constantArray = Constants::getArray();
编辑
自从PHP5.4以来,甚至可以在不需要中间变量的情况下访问数组值,即以下方式是有效的:
$x = Constants::getArray()['index'];
const AtomicValue =42; public static $fooArray = ('how','di')
- Frank N我使用它的方式如下。我希望它能帮助其他人。
config.php
class app{
private static $options = array(
'app_id' => 'hello',
);
public static function config($key){
return self::$options[$key];
}
}
我需要在文件中使用常量。
require('config.php');
print_r(app::config('app_id'));
这是我使用的方法。它与 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;
}
}
就这样使用:
Constants::getArray(); // Full array
// OR
Constants::getArray(1); // Value of 1 which is 'orange'
自 PHP 7 版本开始,你可以使用 define() 函数来定义一个常量数组:
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // outputs "cat"
您可以将其存储为常量的JSON字符串。从应用程序的角度来看,JSON在其他情况下也可能很有用。
define ("FRUITS", json_encode(array ("apple", "cherry", "banana")));
$fruits = json_decode (FRUITS);
var_dump($fruits);
甚至可以与关联数组一起使用,例如在类中。
class Test {
const
CAN = [
"can bark", "can meow", "can fly"
],
ANIMALS = [
self::CAN[0] => "dog",
self::CAN[1] => "cat",
self::CAN[2] => "bird"
];
static function noParameter() {
return self::ANIMALS[self::CAN[0]];
}
static function withParameter($which, $animal) {
return "who {$which}? a {$animal}.";
}
}
echo Test::noParameter() . "s " . Test::CAN[0] . ".<br>";
echo Test::withParameter(
array_keys(Test::ANIMALS)[2], Test::ANIMALS["can fly"]
);
// dogs can bark.
// who can fly? a bird.
我知道这是一个有点老的问题,但这是我的解决方案:
<?php
class Constant {
private $data = [];
public function define($constant, $value) {
if (!isset($this->data[$constant])) {
$this->data[$constant] = $value;
} else {
trigger_error("Cannot redefine constant $constant", E_USER_WARNING);
}
}
public function __get($constant) {
if (isset($this->data[$constant])) {
return $this->data[$constant];
} else {
trigger_error("Use of undefined constant $constant - assumed '$constant'", E_USER_NOTICE);
return $constant;
}
}
public function __set($constant,$value) {
$this->define($constant, $value);
}
}
$const = new Constant;
我定义它是因为我需要在常量中存储对象和数组,所以我还安装了php的runkit,这样我就可以使$const变量成为超级全局变量。
你可以这样使用它:$const->define("my_constant",array("my","values"));
或者只需 $const->my_constant = array("my","values");
要获取值,只需简单地调用$const->my_constant;
__get
和 __set
... 我必须承认这种方法很棒。 - user5147563define('TEAM', ['guy', 'development team']);
echo TEAM[0];
// output from system will be "guy"