Do objects maintain identity under all non-cloning conditions in PHP?
Posted
by
Buttle Butkus
on Programmers
See other posts from Programmers
or by Buttle Butkus
Published on 2014-06-08T02:57:53Z
Indexed on
2014/06/08
3:38 UTC
Read the original article
Hit count: 265
PHP 5.5
I'm doing a bunch of passing around of objects with the assumption that they will all maintain their identities - that any changes made to their states from inside other objects' methods will continue to hold true afterwards. Am I assuming correctly?
I will give my basic structure here.
class builder {
protected $foo_ids = array(); // set in construct
protected $foo_collection;
protected $bar_ids = array(); // set in construct
protected $bar_collection;
protected function initFoos() {
$this->foo_collection = new FooCollection();
foreach($this->food_ids as $id) {
$this->foo_collection->addFoo(new foo($id));
}
}
protected function initBars() {
// same idea as initFoos
}
protected function wireFoosAndBars(fooCollection $foos, barCollection $bars) {
// arguments are passed in using $this->foo_collection and $this->bar_collection
foreach($foos as $foo_obj) { // (foo_collection implements IteratorAggregate)
$bar_ids = $foo_obj->getAssociatedBarIds();
if(!empty($bar_ids) ) {
$bar_collection = new barCollection(); // sub-collection to be a component of each foo
foreach($bar_ids as $bar_id) {
$bar_collection->addBar(new bar($bar_id));
}
$foo_obj->addBarCollection($bar_collection);
// now each foo_obj has a collection of bar objects, each of which is also in the main collection. Are they the same objects?
}
}
}
}
What has me worried is that foreach
supposedly works on a copy of its arrays. I want all the $foo and $bar objects to maintain their identities no matter which $collection object they become of a part of. Does that make sense?
© Programmers or respective owner