Voting

: five plus four?
(Example: nine)

The Note You're Voting On

Marius
10 years ago
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
*/
?>

<< Back to user notes page

To Top