3

如何从以下对象数组访问项目数组

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] => 
    )

)
4

3 回答 3

4

如果您必须访问私有/受保护的类属性,您可以简单地使用魔术__get方法。在这种情况下,反射将太过分了。不过,在这种情况下使用魔术方法是否具有良好的设计意义取决于您的情况。

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属性。

于 2012-05-10T14:35:58.077 回答
1

可能是这样的:

$object->_items[index]->_productId

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

    $reflectionObject = new ReflectionObject($yourObject);
    $property = $reflectionObject->getProperty('_items');
    $property->setAccessible(true);
    $items = $property->getValue($yourObject);
于 2012-05-10T14:27:23.323 回答
0

尝试以下操作:

$object->$first_node->$second_node->$third_node;
于 2012-05-10T14:26:08.500 回答