Edit report at https://bugs.php.net/bug.php?id=46492&edit=1
ID: 46492
Comment by: gnutix at gmail dot com
Reported by: email at davekok dot nl
Summary: array instanceof Traversable results false
Status: Not a bug
Type: Bug
Package: SPL related
Operating System: linux 2.6
PHP Version: 5.2.6
Block user comment: N
Private report: N
New Comment:
I agree with "v_cadet at yahoo dot fr"'s comment.
We should clearly have a simple way to check if a variable is "traversable",
either via a function and an interface.
It would therefore simplify the foreach()'s "encapsulating condition" avoiding
the too-well-known "PHP Warning: Invalid argument supplied for foreach()"
error,
and admit for parameters in function to throw Exception if they're not
traversable.
Previous Comments:
------------------------------------------------------------------------
[2012-05-17 18:32:11] v_cadet at yahoo dot fr
Sorry but this report *does* make quite a lot of sense. For instance a function
may expect any traversable object as an argument, which makes PHP automatically
validate calls to that function. Example:
function foo( Traversable $item_list ) { /* Do something */ }
Since arrays *are* traversable somehow, this simple function would make the
code quite clear and obvious, automatically accept a small range of possible
argument types and yielding runtime exception otherwise. Now since arrays are
not objects, one must bloat the code and wrap arrays into Traversable objects.
Granted this can be achieved in a very simple way but this results in uselessly
adding code, regardless of how simple it can be.
I have run into the need of such a nice feature in a recent PHP application. In
my case I have removed the Traversable hint and added checks (as you suggested)
against the argument for acceptable types. But I for one yet don't like that
workaround.
------------------------------------------------------------------------
[2008-11-05 11:08:00] [email protected]
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
array() is not an object of any class. Hence, it can't implement any interface
such as Traversable.
You probably want: is_array($o) || $o instanceof Traversable
------------------------------------------------------------------------
[2008-11-05 09:37:37] email at davekok dot nl
Description:
------------
When executing the expression
(array() instanceof Traversable)
it results in false.
I'm not really sure if an array is seen as an object in PHP. But since there is
no function like is_traversable. I expected this to work.
Reproduce code:
---------------
$a = array();
return ($a instanceof Traversable)
Expected result:
----------------
true
Actual result:
--------------
false
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=46492&edit=1