2011-12-08 6 views
1

クラスのすべてのデータメンバーと関数を反復し、継承されているものをチェックする最良の方法は何ですか?あなたはすべてのパブリックおよび保護されたプロパティを一覧表示することができ、これに例えばhttp://php.net/manual/en/book.reflection.phpオブジェクトのすべてのメンバーをリストする方法と継承されたメンバーを知る方法

+1

[このヘルプをしますか?](http://php.net/manual/en /book.reflection.php) –

答えて

1

これを行うには、reflectionを使用する必要があります。継承されたプロパティを取得するには、すべての親を手動で参照する必要があるようです。ここでは...良いスタートコメントはテイクダウンを受ける場合には、コードをコピーし

かもしれcomment on php.net

function getClassProperties($className, $types='public'){ 
    $ref = new ReflectionClass($className); 
    $props = $ref->getProperties(); 
    $props_arr = array(); 
    foreach($props as $prop){ 
     $f = $prop->getName(); 

     if($prop->isPublic() and (stripos($types, 'public') === FALSE)) continue; 
     if($prop->isPrivate() and (stripos($types, 'private') === FALSE)) continue; 
     if($prop->isProtected() and (stripos($types, 'protected') === FALSE)) continue; 
     if($prop->isStatic() and (stripos($types, 'static') === FALSE)) continue; 

     $props_arr[$f] = $prop; 
    } 
    if($parentClass = $ref->getParentClass()){ 
     $parent_props_arr = getClassProperties($parentClass->getName());//RECURSION 
     if(count($parent_props_arr) > 0) 
      $props_arr = array_merge($parent_props_arr, $props_arr); 
    } 
    return $props_arr; 
} 
+0

問題が解決したようですが、continueキーワードを使用したこれらのif文が意味することを説明してください。 –

1

は、反射を見てください

$foo = new Foo(); 

$reflect = new ReflectionClass($foo); 
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED); 
var_dump($props); 

あなたは親クラスを取得するためにReflectionClass::getParentClassを使用して、プロパティを比較することができますあなたのクラスの親クラスのプロパティを継承したものを見ることができます。

関連する問題