note 128026 added to splobjectstorage.detach

From: Date: Sat, 17 Dec 2022 09:56:08 +0000
Subject: note 128026 added to splobjectstorage.detach
Groups: php.notes 
Request: Send a blank email to php-notes+get-219982@lists.php.net to get a copy of this message
There was problematic case when detaching object inside loop, because SplObjectStorage need to call next() before detach() Example using foreach : <?php /** * spl object storage bugs when looping * @see https://bugs.php.net/bug.php?id=65629 */ $firstStorage = new SplObjectStorage(); $secondStorage = new SplObjectStorage(); // temporary storage $temporaryStorage = new SplObjectStorage(); // range 0 - 9 $range = range(0, 9); foreach ($range as $id) { $object = new stdClass(); $object->id = $id; $firstStorage->attach($object); $secondStorage->attach($object); } // direct detach inside loop foreach ($firstStorage as $storage) { // on common array it will detach all even in loop // but object storage still remain 1 object if ($storage->id < 5) { $firstStorage->detach($storage); } } // collect storage into temporary splObjectStorage foreach ($secondStorage as $storage) { // collect into temporary storage if ($storage->id < 5) { $temporaryStorage->attach($storage); } } // removeAll by temporary Storage $secondStorage->removeAll($temporaryStorage); var_dump(count($firstStorage)); // int(6) var_dump(count($secondStorage)); // int(5) ?> Example using while : <?php $firstStorage = new SplObjectStorage(); $secondStorage = new SplObjectStorage(); // temporary storage $temporaryStorage = new SplObjectStorage(); // range 0 - 9 $range = range(0, 9); foreach ($range as $id) { $object = new stdClass(); $object->id = $id; $firstStorage->attach($object); $secondStorage->attach($object); } $firstStorage->rewind(); while ($firstStorage->valid() && ($current = $firstStorage->current())) { if ($current->id < 5) { $firstStorage->detach($current); } // don't call next after detach $firstStorage->next(); } $secondStorage->rewind(); while ($secondStorage->valid() && ($current = $secondStorage->current())) { // call next behavior before detach $secondStorage->next(); if ($current->id < 5) { $secondStorage->detach($current); } } var_dump(count($firstStorage)); // int(6) var_dump(count($secondStorage)); // int(5) ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.175 (proxied: 125.166.1.152) ---- Manual Page -- https://php.net/manual/en/splobjectstorage.detach.php Edit -- https://main.php.net/note/edit/128026 Del: integrated -- https://main.php.net/note/delete/128026/integrated Del: useless -- https://main.php.net/note/delete/128026/useless Del: bad code -- https://main.php.net/note/delete/128026/bad+code Del: spam -- https://main.php.net/note/delete/128026/spam Del: non-english -- https://main.php.net/note/delete/128026/non-english Del: in docs -- https://main.php.net/note/delete/128026/in+docs Del: other reasons-- https://main.php.net/note/delete/128026 Reject -- https://main.php.net/note/reject/128026 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219982) next »