私は定数のリストを持つクラスを持っています。get_object_vars
定数を配列にダンプするようなものが欲しいです。それを行う最も簡単な方法は何ですか?
前もって感謝します!
これには 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