这个失败:
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);
这似乎是不必要的努力。
当前回答
使用爆炸和内爆函数,我们可以临时想出一个解决方案:
$array = array('lastname', 'email', 'phone');
define('DEFAULT_ROLES', implode (',' , $array));
echo explode(',' ,DEFAULT_ROLES ) [1];
这将回复邮件。
如果你想让它更优化,你可以定义2个函数来做重复的事情,像这样:
//function to define constant
function custom_define ($const , $array) {
define($const, implode (',' , $array));
}
//function to access constant
function return_by_index ($index,$const = DEFAULT_ROLES) {
$explodedResult = explode(',' ,$const ) [$index];
if (isset ($explodedResult))
return explode(',' ,$const ) [$index] ;
}
希望这能有所帮助。快乐编码。
其他回答
从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'));
如果你使用的是php7 & 7+,你也可以像这样使用fetch
define('TEAM', ['guy', 'development team']);
echo TEAM[0];
// output from system will be "guy"
甚至可以与关联数组工作..例如在课堂上。
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.
如果您是从2009年开始看这个,并且不喜欢AbstractSingletonFactoryGenerators,那么这里有一些其他选项。
记住,数组在赋值时被“复制”,或者在本例中被返回,因此实际上每次都得到相同的数组。(参见PHP中数组的写时复制行为。)
function FRUITS_ARRAY(){
return array('chicken', 'mushroom', 'dirt');
}
function FRUITS_ARRAY(){
static $array = array('chicken', 'mushroom', 'dirt');
return $array;
}
function WHAT_ANIMAL( $key ){
static $array = (
'Merrick' => 'Elephant',
'Sprague' => 'Skeleton',
'Shaun' => 'Sheep',
);
return $array[ $key ];
}
function ANIMAL( $key = null ){
static $array = (
'Merrick' => 'Elephant',
'Sprague' => 'Skeleton',
'Shaun' => 'Sheep',
);
return $key !== null ? $array[ $key ] : $array;
}
我知道这是一个有点老的问题,但这是我的解决方案:
<?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;
我定义它是因为我需要在常量中存储对象和数组,所以我也安装了runkit到php,这样我就可以使$const变量超全局。
你可以使用它作为$const->define("my_constant",array("my","values"));或者只是$const->my_constant = array("my","values");
要获取该值,只需调用$const->my_constant;