これは失敗しました:
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'];
define()
PHP 7 を使用している場合は、最初に試したのと同じように、最終的に を使用できます。
<?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);
それらをクラスの静的変数として保存できます。
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';
他の人が配列を変更できるという考えが気に入らない場合は、ゲッターが役立つ場合があります。
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'];
こんな感じで使っています。他の人に役立つことを願っています。
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'
JSON 文字列として定数に格納できます。アプリケーションの観点からすると、JSON は他の場合にも役立ちます。
define ("FRUITS", json_encode(array ("apple", "cherry", "banana")));
$fruits = json_decode (FRUITS);
var_dump($fruits);
PHP 5.6 以降では、const
以下のようなキーワードを使用して定数配列を定義できます。
const DEFAULT_ROLES = ['test', 'development', 'team'];
以下のように、さまざまな要素にアクセスできます。
echo DEFAULT_ROLES[1];
....
PHP 7 以降では、以下のように定数配列を定義できdefine
ます。
define('DEFAULT_ROLES', [
'test',
'development',
'team'
]);
以前と同じ方法でさまざまな要素にアクセスできます。
はい、配列を定数として定義できます。PHP 5.6 以降では、定数をスカラー式として定義することが可能になり、配列定数を定義することも可能になりました。定数をリソースとして定義することは可能ですが、予期しない結果を引き起こす可能性があるため、避ける必要があります。
<?php
// Works as of PHP 5.3.0
const CONSTANT = 'Hello World';
echo CONSTANT;
// Works as of PHP 5.6.0
const ANOTHER_CONST = CONSTANT.'; Goodbye World';
echo ANOTHER_CONST;
const ANIMALS = array('dog', 'cat', 'bird');
echo ANIMALS[1]; // outputs "cat"
// Works as of PHP 7
define('ANIMALS', array(
'dog',
'cat',
'bird'
));
echo ANIMALS[1]; // outputs "cat"
?>
このリンクを参考に
楽しいコーディングを。
少し古い質問だと思いますが、ここに私の解決策があります:
<?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;
このように定義できます
define('GENERIC_DOMAIN',json_encode(array(
'gmail.com','gmail.co.in','yahoo.com'
)));
$domains = json_decode(GENERIC_DOMAIN);
var_dump($domains);
分解および内破機能を使用して、ソリューションを即興で作成できます。
$array = array('lastname', 'email', 'phone');
define('DEFAULT_ROLES', implode (',' , $array));
echo explode(',' ,DEFAULT_ROLES ) [1];
これは反響しemail
ます。
さらに最適化したい場合は、次のように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] ;
}
それが役立つことを願っています。ハッピーコーディング。
定数にはスカラー値のみを含めることができます。配列のシリアル化 (または JSON エンコード表現) を保存することをお勧めします。