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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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: Tue Apr 30 10:01:29 2024 UTC