PHPverse 2025

Voting

: max(six, three)?
(Example: nine)

The Note You're Voting On

Adam Monsen
11 years ago
Note some inconsistent/surprising behavior in SplObjectStorage to preserve backwards compatibility. You can't properly use foreach with key/value syntax.

<?php
$spl
= new SplObjectStorage ();
$keyForA = new StdClass();
$keyForB = new StdClass();
$spl[$keyForA] = 'value a';
$spl[$keyForB] = 'value b';
foreach (
$spl as $key => $value)
{
// $key is NOT an object, $value is!
// Must use standard array access to get strings.
echo $spl[$value] . "\n"; // prints "value a", then "value b"
}
// it may be clearer to use this form of foreach:
foreach ($spl as $key)
{
// $key is an object.
// Use standard array access to get values.
echo $spl[$key] . "\n"; // prints "value a", then "value b"
}
?>

See https://bugs.php.net/bug.php?id=49967

<< Back to user notes page

To Top