使用ReflectionClass读取类Properties

时间:2011-11-03 11:31:51

标签: php

我试图通过ReflectionClass阅读以下类的属性 现在问题是tradingMarketTradingMarket类的实例。 如果变量是Object,那么我想调用该对象上的一个函数,该函数也返回所述类的属性。

class Certificate {
      private $isin;
      private $tradingMarket;
      private $currency;
      private $issuer;
      private $issuingPrice;
      private $currentPrice;
      private $priceHistory;
      private $documents;
      private $serializer;
}

1 个答案:

答案 0 :(得分:0)

您可以使用递归(未测试):

function getProperties($object) {
    $ref = new ReflectionClass(get_class($object));
    foreach ($reg->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE) as $prop) {
        if (is_object($prop->getValue($object)) getProperties($prop->getValue($object));
        else echo get_class($object)." => ".$prop->getName();
    }
}