6

Lets say I have these classes:

class Foo {
   public $_data;
   public function addObject($obj) {
        $this->_data['objects'][] = $obj;
   }
}

class Bar {
    public $_data;
    public function __construct() {
        $this->_data['value'] = 42;
    }
    public function setValue($value) {
        $this->_data['value'] = $value;
    }
}

$foo = new Foo();
$bar = new Bar();
$foo->addObject($bar);
foreach($foo->_data['objects'] as $object) {
    $object->setValue(1);
}
echo $foo->_data['objects'][0]->_data['value']; //42

My actual code is this, very similar, uses ArrayAccess:

foreach($this->_data['columns'] as &$column) {
                $filters = &$column->getFilters();
                foreach($filters as &$filter) {
                    $filter->filterCollection($this->_data['collection']);
                }
            }

filterCollection changes a value in $filter, but when you look at the $this object, the value is not right.

4
  • you are changing value of this variable: $object. It's not the one you are outputing later ($foo->_data['objects'][0]). Commented Feb 2, 2011 at 6:26
  • You should be getting a 1. If you ran this in PHP 4 it would cause parse errors with the public keywords, if you ran this in PHP 5 it would work as you expect. I tested your code and I got 1. Commented Feb 2, 2011 at 6:29
  • I know that objects are passed around by reference, yet thought that maybe foreach loop somehow is different. Can't test it right now. Anyway, thanks for clarifying this ;) Commented Feb 2, 2011 at 6:35
  • 1
    @egis: It's the default behavior; objects are only copied on clone $object, and even then it does only a shallow copy. Commented Feb 2, 2011 at 6:38

3 Answers 3

10
foreach($foo->_data['objects'] as &$object) {
    $object->setValue(1);
}

Notice the &

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

3 Comments

I tried the & and it didnt work. The problem is my example is simplified. I put my actual code in my question.
Actually, the & isn't necessary here. $object already represents references to the same objects.
This is correct for the question I asked, however, my issue ended up being with incorrect session initialization.
1

Foreach operates on a copy of the array. Use an & before the object variable.

foreach($foo->_data['objects'] as &$object)

1 Comment

Yes, it operates on a copy of the array; however this copy holds references to the same objects.
0

PHP paradigm is that objects (and resources) are always references, while other types (base types or arrays) are copied, so the & operator has no effect on objects (and is meaningless on resources since only "special functions" i.e. external library modules can take them as parameters), but allows to pass variables of other types by reference.

Comments

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.