【问题标题】:php how to access object arrayphp如何访问对象数组
【发布时间】:2012-05-10 14:24:14
【问题描述】:

如何从下面的对象数组中访问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] => 
    )

)

【问题讨论】:

  • 是否可以在 Cart 类中添加公共 getItems() 函数?
  • 您是在问,“我如何访问私有/受保护的对象属性?” -或- “我如何让对象表现得像一个数组? "
  • 我要求访问这些项目

标签: php arrayobject


【解决方案1】:

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

【讨论】:

  • 我同意这种过度的说法,但考虑到我们不知道类接口的事实,我们应该考虑它。这就是为什么反射是为了,我猜。恕我直言
【解决方案2】:

可能是这样的:

$object->_items[index]->_productId

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

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

【讨论】:

  • $object->_items 应该返回 items 数组。但事实并非如此。我如何获得项目数组。是否需要将其转换为公共
  • 一件事,这段代码在本地机器上运行良好,但在实时服务器上出现问题。有什么线索吗?
  • 什么样的问题?确保两台机器都运行相同的 php 版本
【解决方案3】:

尝试以下方法:

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

【讨论】:

  • 它是私有的,所以可能不是一个选项。
猜你喜欢
  • 2020-05-09
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
相关资源
最近更新 更多