2012-05-10 74 views
3

如何从下面的对象数组访问项目阵列PHP如何访问对象数组

Cart66Cart Object 
(
[_items:Cart66Cart:private] => Array 
    (
     [2] => Cart66CartItem Object 
      (
       [_productId:Cart66CartItem:private] => 327 
       [_quantity:Cart66CartItem:private] => 3 
       [_optionInfo:Cart66CartItem:private] => 
       [_priceDifference:Cart66CartItem:private] => 0 
       [_customFieldInfo:Cart66CartItem:private] => 
       [_productUrl:Cart66CartItem:private] => http://localhost/odesk/cart66/fran-wilson-aloe-lip-care/ 
       [_formEntryIds:Cart66CartItem:private] => Array 
        (
        ) 

      ) 

     [3] => Cart66CartItem Object 
      (
       [_productId:Cart66CartItem:private] => 368 
       [_quantity:Cart66CartItem:private] => 2 
       [_optionInfo:Cart66CartItem:private] => 
       [_priceDifference:Cart66CartItem:private] => 0 
       [_customFieldInfo:Cart66CartItem:private] => 
       [_productUrl:Cart66CartItem:private] => http://localhost/odesk/cart66/beauty-strokes-basic-shadow-brush/ 
       [_formEntryIds:Cart66CartItem:private] => Array 
        (
        ) 

      ) 

    ) 

[_promotion:Cart66Cart:private] => 
[_promoStatus:Cart66Cart:private] => 0 
[_shippingMethodId:Cart66Cart:private] => 13 
[_liveRates:Cart66Cart:private] => Cart66LiveRates Object 
    (
     [toZip] => 
     [weight] => 
     [rates] => Array 
      (
      ) 

     [_toCountryCode:protected] => 
    ) 

) 
+0

是否有可能将公共getItems()函数添加到Cart类中? – gunnx

+0

你是问,*“如何访问一个私有/受保护的对象属性?”* - 或 - *“我如何使对象像一个数组一样行事?”* – rdlowrey

+0

我要求访问的项目 – nbhatti2001

回答

1

事情是这样的,也许:

$object->_items[index]->_productId 

但是如果_items是私人的,你需要一个公共的getter或者与Reflection类混淆。您可以设置私有财产通过ReflectionProperty 可访问试试这个:

$reflectionObject = new ReflectionObject($yourObject); 
    $property = $reflectionObject->getProperty('_items'); 
    $property->setAccessible(true); 
    $items = $property->getValue($yourObject); 
+0

$ object - > _ items应该返回items数组。但事实并非如此。我如何获得物品数组。需要将其转换为公共 – nbhatti2001

+0

查看更新的答案 –

+0

这段代码在本地机器上运行良好,但在实时服务器上给出问题。任何线索? – nbhatti2001

0

尝试以下操作:

$object->$first_node->$second_node->$third_node; 
+0

它是私有的,所以可能不是一个选项。 –

4

如果必须访问私人/保护类属性,你可以简单地使用magic __get method。在这种情况下,反思会过度。不过,在这种情况下使用魔法方法是否具有良好的设计意义取决于你的情况。

class MyClass 
{ 
    private $_items; 

    public function __get($prop) 
    { 
     if ($prop == '_items') { 
      return $this->_items; 
     } 
     throw new OutOfBoundsException; 
    } 
} 

UPDATE

重读看来你以后只是想你的对象表现得像一个数组。为此,您需要执行ArrayAccess并将相关方法指向私有$_items属性。

class MyClass implements ArrayAccess 
{ 
    private $_items = array(); 

    public function __construct() { 
     $this->_items = array(
      "one" => 1, 
      "two" => 2, 
      "three" => 3, 
     ); 
    } 
    public function offsetSet($offset, $value) { 
     if (is_null($offset)) { 
      $this->_items[] = $value; 
     } else { 
      $this->_items[$offset] = $value; 
     } 
    } 
    public function offsetExists($offset) { 
     return isset($this->_items[$offset]); 
    } 
    public function offsetUnset($offset) { 
     unset($this->_items[$offset]); 
    } 
    public function offsetGet($offset) { 
     return isset($this->_items[$offset]) ? $this->_items[$offset] : null; 
    } 
} 

而且最后,PHP有一个内置的ArrayObject类,这将使一个对象的行为非常像一个阵列。你总是可以使用它并将相关方法指向私有的$_items属性。

+0

我同意overboard声明,但鉴于我们不知道类接口,我们应该考虑它。这就是为什么反思,我猜。恕我直言 –

+0

足够公平... – rdlowrey