3

How to access the item array from the following object array

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
  • Is it possible for you to add a public getItems() function into the Cart class? Commented May 10, 2012 at 14:38
  • Are you asking, "How do I access a private/protected object property?" -or- "How do I make an object act like an array?" Commented May 10, 2012 at 14:54
  • I was asking to access the items Commented May 10, 2012 at 14:56

3 Answers 3

4

If you must access a private/protected class property you can simply use the magic __get method. Reflection would be way overboard in this case. Whether or not it makes good design sense to use the magic methods in this case depends on your situation, though.

class MyClass
{
    private $_items;

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

UPDATE

After re-reading it seems you simply want your object to behave like an array. To do this you'll need to implement ArrayAccess and point the relevant methods to the private $_items property.

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;
    }
}

And finally, PHP comes with a built-in ArrayObject class that will make an object behave very much like an array. You could always use that and point the relevent methods at a private $_items property.

Sign up to request clarification or add additional context in comments.

1 Comment

i agree with the overboard statement, but given the fact that we don't know the class interface we should consider it. Thats why reflection is for, i guess. IMHO
1

Something like this maybe:

$object->_items[index]->_productId

But if _items is private you will need a public getter or mess with the Reflection classes. You can set the the private property to be accessible through ReflectionProperty Try this:

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

3 Comments

$object->_items should return the items array. but it is not. how I can get item array. Is need to convert it to public
one thing this code was running well on local machine but giving problem on live server. any clue?
what kind of problems? make sure both machines are running the same php version
0

Try the following:

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

1 Comment

It's private, so probably not an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.