php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46492 array instanceof Traversable results false
Submitted: 2008-11-05 09:37 UTC Modified: 2015-08-28 12:41 UTC
From: email at davekok dot nl Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.2.6 OS: linux 2.6
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: email at davekok dot nl
New email:
PHP Version: OS:

 

 [2008-11-05 09:37 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-05 11:08 UTC] colder@php.net
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
 [2012-05-17 18:32 UTC] 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.
 [2012-07-07 16:36 UTC] gnutix at gmail dot com
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.
 [2015-08-28 12:24 UTC] t dot koehn at outlook dot com
I also agree with `v_cadet at yahoo dot fr` and `gnutix at gmail dot com`

Obviously an array can't be traversable, it's an object, but since we have `callable` type-hints, I guess it would also be possible to add a `iterable` or `traversable` type-hint (Notice the casing)

Notice that the `callable` typehint accepts strings, arrays and Closures freely.

This type-hint would make a `is_array() || instanceof Traversable` check in the background.

Right now you either have to add two methods (e.g. `merge( Traversable $array )` and `mergeArray( array $array )` where the first version wraps the second one with `iterator_to_array` or the second one creates a generator of `$array`

I'd really like to see this feature in PHP and I think it would be a good, fitting and clean addition.
 [2015-08-28 12:41 UTC] requinix@php.net
See also the RFC for union types
https://wiki.php.net/rfc/union_types

and some talk about union typing and/or a pseudo-type on the internals list.
http://marc.info/?l=php-internals&m=143698920517697&w=2 (starting point in the thread)
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Jan 22 16:01:28 2019 UTC