Iterable Type Hint

From: Date: Thu, 12 Jul 2012 14:10:22 +0000
Subject: Iterable Type Hint
Groups: php.internals 
Request: Send a blank email to internals+get-61166@lists.php.net to get a copy of this message
Hello all, At present, there's now way to type hint over a generic structure that it iteratable using foreach(). You can accept arrays using the array hint, and objects using traversable, but you cannot hint both. This yields code that wants to accept that to look like this: function foo($a) { if (!is_array($a) && !$a instanceof Traversable) { raise error; } } Would it be worth while adding a new type hint that checks for this condition? I'd propose Iterable: function foo(Iterable $a) { foreach ($a as ...) {} } It's just a quick thought and wanted some feedback on it. Thanks Anthony

« previous php.internals (#61166) next »