object - php SplObjectStorage Detach() not working -


i have found error in php splobjectstorage detach method. it's working fine in removing object if object comes 1 skip next one.

example:

$s = new splobjectstorage();  $o1 = new stdclass; $o2 = new stdclass; $o3 = new stdclass;  $o1->attr = '1'; $o2->attr = '2'; $o3->attr = '3';  $s->attach($o1); $s->attach($o2); $s->attach($o3);  echo 'removing objects...<pre>'; var_dump($s->count()); foreach ($s $obj) {    var_dump($obj->attr);    if($obj->attr == 2 || $obj->attr == 1) {        echo "deleting...".$obj->attr;        $s->detach($obj);    } }  echo 'checking objects....'; var_dump($s->count()); foreach ($s $obj) {    var_dump($obj->attr); } 

and gives me result. shouldn't be, because want delete object (attr == 1) , object (attr == 2) both. detach() method delete first object, skip next 1 , loop.

removing objects...  int 3  string '1' (length=1)  deleting...1  string '3' (length=1)   checking objects....  int 2  string '2' (length=1)  string '3' (length=1) 

** object ($o2->attr = '2') should deleted, not because skip in first loop.

i have found bug in splobjectstorage detach() method are:

detaching current entry storage prevents splobjectstorage::next() operate.

so preventing next object when detach() execute, iteration never reach second (next) stdclass object. splobjectstorage::next() relies on current element valid.

to avoid bug in splobjectstorage class needs call next() before calling detach().

hope other users not face problem , not waste time...


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -