From:             alan dot bem at gmail dot com
Operating system: 
PHP version:      Irrelevant
Package:          SPL related
Bug Type:         Bug
Bug description:SplObjectsStorage::detach() rewinds internal pointer

Description:
------------
It seems that SplObjectsStorage::detach() internally rewinds its array
pointer.

PHP versions from 5.1.0 to 5.5.3 are affected.

Test script:
---------------
// To see this live, go to: http://3v4l.org/MAELj

<?php
class A
{
    public $val;
    public function __construct($val) { $this->val = $val; }
}
$storage = new SplObjectStorage;
for($i = 1; $i <= 10; $i++) {
        $storage->attach(new A($i));
}
$iterations = 0;
$storage->rewind();
while ($storage->valid()) {
    $iterations++;
    $object = $storage->current();
    echo 'Iteration #' . $iterations . ' with object A(' . $object->val .
')';
    $storage->next();
    if($iterations === 2 || $iterations === 8) {
        $storage->detach($object);
        echo ' - deleted Object A(' . $object->val . ') ';
    }
    echo PHP_EOL;
}
echo 'Number of iterations: ' . $iterations . PHP_EOL;

Expected result:
----------------
Iteration #1 with object A(1)
Iteration #2 with object A(2) - deleted Object A(2) 
Iteration #3 with object A(3)
Iteration #4 with object A(4)
Iteration #5 with object A(5)
Iteration #6 with object A(6)
Iteration #7 with object A(7) - deleted Object A(7) 
Iteration #8 with object A(8)
Iteration #9 with object A(9)
Iteration #10 with object A(10)
Number of iterations: 10

Actual result:
--------------
Iteration #1 with object A(1)
Iteration #2 with object A(2) - deleted Object A(2) 
Iteration #3 with object A(1)
Iteration #4 with object A(3)
Iteration #5 with object A(4)
Iteration #6 with object A(5)
Iteration #7 with object A(6)
Iteration #8 with object A(7) - deleted Object A(7) 
Iteration #9 with object A(1)
Iteration #10 with object A(3)
Iteration #11 with object A(4)
Iteration #12 with object A(5)
Iteration #13 with object A(6)
Iteration #14 with object A(8)
Iteration #15 with object A(9)
Iteration #16 with object A(10)
Number of iterations: 16

-- 
Edit bug report at https://bugs.php.net/bug.php?id=65629&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=65629&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=65629&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=65629&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=65629&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=65629&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=65629&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=65629&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=65629&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=65629&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=65629&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=65629&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=65629&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=65629&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65629&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=65629&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=65629&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=65629&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65629&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=65629&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=65629&r=mysqlcfg

Reply via email to