|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-10-04 14:47 UTC] zhuk2205 at gmail dot com
Description:
------------
If create object and implement it ArrayAccess and Iterator interfaces, we have a problem with remove elements from storage with use ArrayAccess.
Likely PHP \Iterator system not find "active" key after remove active element, and use key as active next key. As result, in next iteration will be invalid key.
Test script:
---------------
<?php
class MyModel
{
}
class Collection implements \ArrayAccess, \Iterator
{
private $storage = array();
public function offsetExists($offset)
{
return isset($this->storage[$offset]);
}
public function offsetGet($offset)
{
return $this->storage[$offset];
}
public function offsetSet($offset, $value)
{
if (null === $offset) {
$this->storage[] = $value;
} else {
$this->storage[$offset] = $value;
}
}
public function offsetUnset($offset)
{
unset ($this->storage[$offset]);
}
public function current()
{
return current($this->storage);
}
public function next()
{
return next($this->storage);
}
public function key()
{
return key($this->storage);
}
public function valid()
{
return key($this->storage) !== null;
}
public function rewind()
{
reset($this->storage);
}
public function count()
{
return count($this->storage);
}
}
class MyCollectionModel
{
private $collection;
public function __construct()
{
$this->collection = new Collection();
}
public function addElement($element)
{
$this->collection[] = $element;
}
public function removeMyModels()
{
foreach ($this->collection as $index => $value) {
if ($value instanceof MyModel) {
unset ($this->collection[$index]);
}
}
}
}
// Create collection
$myCollection = new MyCollectionModel();
// Add own models to collections
for ($i = 0; $i < 10; $i++) {
$myCollection->addElement(new MyModel());
}
// Remove owl models from collection
$myCollection->removeMyModels();
// So, not all elements removed
print_r($myCollection);
Expected result:
----------------
Remove all "MyModel" instances from collection.
Actual result:
--------------
Not all removed objects.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 16:00:02 2025 UTC |
You can get the same behaviour with this code: $a = [1,2,3]; for (reset($a); ($key = key($a)) !== null; next($a)) { unset($a[$key]); } var_dump($a); // [1] => 2 It's not recommended practice to remove elements from a collection while you iterate over it; therefore I don't think we can consider this to be a bug.