|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 11:00:01 2025 UTC |
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.