2011-07-03 11 views
1

だが、私はこのクラスを持っているとしましょう:PHP:リフレクションによって静的フィールド/プロパティをリストする方法は?

class Example {  
    public static $FOO = array('id'=>'foo', 'length'=>23, 'height'=>34.2); 
    public static $BAR = array('id'=>'bar', 'length'=>22.5, 'height'=>96.223); 
} 

は、どのように私は、静的フィールドのリストを取得するためにリフレクションを使用することができますか? (配列( '$ FOO'、 '$ BAR')のようなもの?)

答えて

1

あなたは[ReflectionClass][1]]を使いたいと思うでしょう。 getProperties()関数は、ReflectionPropertyオブジェクトの配列を返します。 ReflectionPropertyオブジェクトには、プロパティが静的かどうかを示すisStatic()メソッドと、名前を返すメソッドgetName()があります。

例:

<?php 

class Example {  
    public static $FOO = array('id'=>'foo', 'length'=>23, 'height'=>34.2); 
    public static $BAR = array('id'=>'bar', 'length'=>22.5, 'height'=>96.223); 
} 

$reflection = new ReflectionClass('Example'); 
$properties = $reflection->getProperties(); 
$static = array(); 

if (! empty($properties)) 
    foreach ($properties as $property) 
    if ($property->isStatic()) 
     $static[] = $property->getName(); 

print_r($static); 
+0

、フランソワをありがとう! – Cambiata

+0

@Cambiata - ようこそ。 –

関連する問題