Bug #68153 [NEW]: Iteration error, if use object with ArrayAccess
| From: | zhuk2205 at gmail dot com | Date: | Sat, 04 Oct 2014 14:47:35 +0000 |
| Subject: | Bug #68153 [NEW]: Iteration error, if use object with ArrayAccess | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-187849@lists.php.net to get a copy of this message | ||
From: zhuk2205 at gmail dot com
Operating system: Mac OS
PHP version: 5.5.17
Package: SPL related
Bug Type: Bug
Bug description:Iteration error, if use object with ArrayAccess
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.
--
Edit bug report at https://bugs.php.net/bug.php?id=68153&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68153&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68153&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68153&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68153&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68153&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68153&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68153&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68153&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68153&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68153&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68153&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68153&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68153&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68153&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68153&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68153&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68153&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68153&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68153&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68153&r=mysqlcfg