|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70364 foreach type hinting
Submitted: 2015-08-26 16:21 UTC Modified: 2017-08-05 04:48 UTC
Avg. Score:3.1 ± 1.8
Reproduced:5 of 6 (83.3%)
Same Version:5 (100.0%)
Same OS:4 (80.0%)
From: untuned20 at gmail dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-08-26 16:21 UTC] untuned20 at gmail dot com
Allow for the ability to type hint a foreach loop.

Test script:

$classList = [/* List of classes */];

foreach($classList as $index => MyClass $class){}
foreach($classList as MyClass $class){}

class MyClass{
    /* Methods and Properties */


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-26 19:25 UTC]
-Package: PHP Language Specification +Package: Scripting Engine problem
 [2015-09-03 01:37 UTC]
Do you have problem with 

foreach($classList as $class) {
  assert($class instanceof MyClass);

or something like this?
 [2015-09-03 01:52 UTC] untuned20 at gmail dot com
I am seeing that php is trying to get more and more into type hinting, and that it would be nice to have type hinting in foreach loops as well as in functions. Not only would it give the same functionality as when declaring a function, but it would also allow IDE's to autocomplete nicely.
 [2015-09-09 11:08 UTC] eliasvo at gmail dot com
I, for one, don't see the need for typed language constructs (heck, even languages like C++ have introduced the auto keyword because iterating over complex types was getting way to verbose). However, if this is something that is going to be implemented, there should be more thought put into this. Things to consider:

- What is the expected behaviour if the type constraint is not met (catchable fatal, exception, warning, notice, ...?)
- What about allowing multiple types? Do we need to introduce the vague mixed type-hint?
- Scalars... do we implement type-hints for the keys, too (foreach ($array as int $key => string $value))
- If key type-hints are made possible, what do they do: the int $key hint should only allow numeric indexes or b) should skip over any key that is not numeric c) convert numeric strings to ints <- should this change the hashtable we're iterating over (probably not), but it might cause issues later on if it doesn't

This request needs a lot more thought put into it, but really, there's no need as there are plenty of other ways to achieve this already:

    array_map($instanceList, function (MyClass $instance) {
        //element is guaranteed to be instance of MyClass here
 [2017-08-05 04:48 UTC]
-Status: Open +Status: Suspended
 [2017-08-05 04:48 UTC]
Thank you for your interest in PHP and for submitting a feature request. Please be aware that due to the magnitude of change this request requires, it would be necessary to discuss it on PHP Internals list ( as an RFC. Please read the guide about creating RFCs here:
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the Internals list ( and/or solicit help from one of the experienced developers. 

Please to not consider this comment as a negative view on the merits of your proposal - every proposal which requires changes of certain magnitude, even the very successful and widely supported ones, must be done through the RFC process. This helps make the process predictable, transparent and accessible to all developers.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Jan 21 19:01:27 2021 UTC