php如何访问对象数组

时间:2012-05-10 14:24:15

标签: php arrayobject

如何从以下对象数组

访问item数组
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] => 
    )

)

3 个答案:

答案 0 :(得分:4)

如果必须访问私有/受保护的类属性,您只需使用magic __get method即可。在这种情况下,反思会过于夸张。在这种情况下,使用魔术方法是否具有良好的设计意义取决于你的情况。

class MyClass
{
    private $_items;

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

<强>更新

重新阅读后,您似乎只希望对象的行为类似于数组。为此,您需要实现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属性。

答案 1 :(得分:1)

这样的事情可能是:

$object->_items[index]->_productId

但是如果_items是私有的,你将需要一个公共的getter或者使用Reflection类。您可以将private属性设置为可通过ReflectionProperty访问 试试这个:

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

答案 2 :(得分:0)

尝试以下方法:

$object->$first_node->$second_node->$third_node;
相关问题