Bug #46492 [Com]: array instanceof Traversable results false

From: Date: Fri, 28 Aug 2015 12:24:35 +0000
Subject: Bug #46492 [Com]: array instanceof Traversable results false
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195592@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=46492&edit=1

 ID:                 46492
 Comment by:         t dot koehn at outlook 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 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.


Previous Comments:
------------------------------------------------------------------------
[2012-07-07 16:36:24] 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.

------------------------------------------------------------------------
[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] 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

------------------------------------------------------------------------
[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


Thread (6 messages)

« previous php.bugs (#195592) next »