PHP:如何获取对象的受保护值?

时间:2015-02-17 11:15:17

标签: php ebay-api

我的电话回来了:print_r($ response)

GetItemResponseType Object
(
    [Item:protected] => ItemType Object
        (
            [ApplicationData:protected] => 651034.9.3011
            [BuyerProtection:protected] => ItemEligible
            [BuyItNowPrice:protected] => AmountType Object
                (
                    [attributeValues] => Array
                        (
                            [currencyID] => EUR
                        )

                    [value:protected] => 0.0
                )

            [Country:protected] => DE

        )

)

我读过这篇文章(How to get protected property of object in PHP),但我无法重现解决方案。

我如何获得国家的价值,即:

   function accessProtected($obj, $prop) {
      $reflection = new ReflectionClass($obj);
      $property = $reflection->getProperty($prop);
      $property->setAccessible(true);
      return $property->getValue($obj);
    }

如果我打电话,我什么也得不到。

echo accessProtected($response, 'Country');

问候,马蒂亚斯

2 个答案:

答案 0 :(得分:0)

您应该在班级中创建公共或静态的getter和setter函数。

答案 1 :(得分:0)

我的问题的答案是:

echo $response->Item->Country;

谢谢。

相关问题