如何访问 PHP 中的对象数组

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

)

你能否在购物车类中添加一个公共的getItems()函数? - gunnx
你是在问:“如何访问私有/受保护的对象属性?”还是“如何使一个对象像一个数组一样工作?” - user895378
我在询问如何访问这些项。 - nbhatti2001
3个回答

4

如果你必须访问一个私有/保护类属性,可以简单地使用magic __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属性。


我同意过度声明,但考虑到我们不知道类接口的事实,我们应该考虑它。这就是反射存在的原因,我想。在我看来。 - Vlad Balmos

1

可能是这样的:

$object->_items[index]->_productId

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

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

$object->_items 应该返回项数组。但它没有返回。我该如何获取项目数组?需要将其转换为公共吗? - nbhatti2001
这段代码在本地机器上运行良好,但在实际服务器上出现问题。有什么线索吗? - nbhatti2001
1
什么样的问题?确保两台机器运行相同的PHP版本。 - Vlad Balmos

0

请尝试以下方法:

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

这是私人的,所以可能不是一个选项。 - kitti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接