php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61312 StdClass doesn't implement Traversable but can be traversed.
Submitted: 2012-03-07 07:44 UTC Modified: 2012-03-07 15:26 UTC
From: levim@php.net Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.4.0 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: levim@php.net
New email:
PHP Version: OS:

 

 [2012-03-07 07:44 UTC] 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] . . .

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-07 15:26 UTC] levim@php.net
-Status: Open +Status: Closed
 [2012-03-07 15:26 UTC] 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 15:28 UTC] hanskrentel at yahoo dot de
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC