18
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

defineでユーザが定義した定数の一覧を取得する

Posted at

マニュアル(get_defined_constants())を見れば書いてあることなんですが、急いでる時になかなか出てこないので、備忘録として。

ユーザ定義の定数を出力するプログラム
define('hoge',1);
$all = get_defined_constants(TRUE);
var_dump( $all['user'] );
出力結果
array(1) {
   ["hoge"]=> int(1)
}

get_defined_constants()関数を使えばいいのですが、引数にTRUEを渡すのがポイント。これで定数が下記のようにカテゴリ化されます。

get_defined_constants(TRUE)の出力結果(一部)
Array
(
    [Core] => Array
        (
            [E_ERROR] => 1
            [E_RECOVERABLE_ERROR] => 4096
            [E_WARNING] => 2
            [E_PARSE] => 4
            [E_NOTICE] => 8
...
        )

    [calendar] => Array
        (
            [CAL_GREGORIAN] => 0
            [CAL_JULIAN] => 1
            [CAL_JEWISH] => 2
            [CAL_FRENCH] => 3
...

ユーザがdefine()で定義した定数は「user」というキーに連想配列で格納されますので、サンプルコードのように$all['user']でユーザ定義定数を取得できるというわけです。

18
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?