0

私は定数のリストを持つクラスを持っています。get_object_vars定数を配列にダンプするようなものが欲しいです。それを行う最も簡単な方法は何ですか?

前もって感謝します!

4

1 に答える 1

4

これには Reflection クラスを使用する必要があります。

function getClassConstants($class) {
    $reflection = new ReflectionClass(
        is_object($class) ? get_class($class) : $class
    );
    return $reflection->getConstants();
}
// usage: $constants = getClassConstants('myClass');
//        $constants = getClassConstants($myClassObjectInstance);

$thisまたは、引数の代わりに渡すことで、クラスのメソッドとして実装できます

ドキュメンテーション

PHP の Reflection クラス - http://us2.php.net/manual/en/class.reflectionclass.php

PHP の ReflectionClass::getConstants - http://us2.php.net/manual/en/reflectionclass.getconstants.php

于 2012-06-15T19:37:40.467 に答える