2012-02-07 18 views
-1

クラスからすべての属性を取得する関数/メソッドはありますか?OO PHP - クラスからすべての属性を取得する

class plantOfTheMonth { 

    //Declare which centre(s) are being used 
    private $centre = ""; 

    //Declare the attributes of the current Plant Of The Month 
    private $name = ""; 
    private $latinName = ""; 
    private $image = ""; 
    private $imageAlt = ""; 
    private $imageLink = ""; 
    private $strapLine = ""; 
    private $description = ""; 
    private $color = ""; 
    private $centres = ""; 

    //Declare variables for error handling 
    private $issue = ""; 
    private $issueCode = ""; 

} 

答えて

4

使用ReflectionClass::getProperties()Reflection APIまたはget_object_vars()機能から:そのような下の私のクラスからすべての属性として。

+1

'get_object_vars()'はアクセス可能なプロパティのみを取得するため、オブジェクトの外部から呼び出されたときにはパブリックプロパティのみが返されます。 – Mchl

+0

ありがとうCrozin、私はクラスの中でこれを回避するために上記の答えのリンクからこの関数を使用しました: 'public function test(){var_dump(get_object_vars($ this)); } ' –

関連する問題