note 116194 added to class.splobjectstorage

From: Date: Thu, 20 Nov 2014 10:54:04 +0000
Subject: note 116194 added to class.splobjectstorage
Groups: php.notes 
Request: Send a blank email to php-notes+get-200963@lists.php.net to get a copy of this message
Do not use SplObjectStorage::detach when forach'ing over items in the storage as this skips the second (and only second) element. Example: <?php class A { public $i; public function __construct($i) { $this->i = $i; } } $container = new \SplObjectStorage(); $container->attach(new A(1)); $container->attach(new A(2)); $container->attach(new A(3)); $container->attach(new A(4)); $container->attach(new A(5)); foreach ($container as $item) { echo $item->i . "\n"; $container->detach($item); } echo "== Left in storage ==\n"; foreach ($container as $item) { echo $item->i . "\n"; } /* Outputs: 1 3 4 5 == Left in storage == 2 */ ?> ---- Server IP: 72.52.91.14 Probable Submitter: 213.197.169.106 ---- Manual Page -- http://php.net/manual/en/class.splobjectstorage.php Edit -- https://master.php.net/note/edit/116194 Del: integrated -- https://master.php.net/note/delete/116194/integrated Del: useless -- https://master.php.net/note/delete/116194/useless Del: bad code -- https://master.php.net/note/delete/116194/bad+code Del: spam -- https://master.php.net/note/delete/116194/spam Del: non-english -- https://master.php.net/note/delete/116194/non-english Del: in docs -- https://master.php.net/note/delete/116194/in+docs Del: other reasons-- https://master.php.net/note/delete/116194 Reject -- https://master.php.net/note/reject/116194 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200963) next »