Bug #61312 [Com]: StdClass doesn't implement Traversable but can be traversed.
| From: | hanskrentel at yahoo dot de | Date: | Wed, 07 Mar 2012 15:28:12 +0000 |
| Subject: | Bug #61312 [Com]: StdClass doesn't implement Traversable but can be traversed. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-168238@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=61312&edit=1
ID: 61312
Comment by: hanskrentel at yahoo dot de
Reported by: levim@php.net
Summary: StdClass doesn't implement Traversable but can be
traversed.
Status: Closed
Type: Bug
Package: Class/Object related
PHP Version: 5.4.0
Block user comment: N
Private report: N
New Comment:
Same for array. It does not "implement" that interface but can be iterated over
in
a foreach loop.
If StdClass *had* the Traverseable interface and foreach would not work, then
I'd
say this is a bug.
See as well the documentation of foreach.
If you want to check foreach-ability: check for object or array *not*
Traversable.
Traversable is an internal type.
Previous Comments:
------------------------------------------------------------------------
[2012-03-07 15:26:23] levim@php.net
Not a bug, really. All objects can be iterated. I had been up too late when I
filed the bug.
------------------------------------------------------------------------
[2012-03-07 07:44:18] levim@php.net
Description:
------------
StdClass does not implement the Traversable interface but can be iterated over in
a foreach loop.
Test script:
---------------
$stdClass = new StdClass;
$stdClass->setUp = function () {
echo "event[setUp] . . . \n";
};
$stdClass->tearDown = function () {
echo "event[tearDown] . . . \n";
};
echo "StdClass instanceof Traversable: ";
echo $stdClass instanceof Traversable
? "Traversable\n"
: "Not traversable\n";
foreach ($stdClass as $key => $value) {
echo "$key => ";
$value();
}
Expected result:
----------------
StdClass instanceof Traversable: Traversable
setUp => event[setUp] . . .
tearDown => event[tearDown] . . .
Actual result:
--------------
StdClass instanceof Traversable: Not traversable
setUp => event[setUp] . . .
tearDown => event[tearDown] . . .
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61312&edit=1