php.net |  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
Votes:4
Avg. Score:3.0 ± 2.0
Reproduced:2 of 3 (66.7%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: untuned20 at gmail dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: Irrelevant OS:
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: untuned20 at gmail dot com
New email:
PHP Version: OS:

 

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

Test script:
---------------
<?php

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

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

class MyClass{
    /* Methods and Properties */
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-26 19:25 UTC] stas@php.net
-Package: PHP Language Specification +Package: Scripting Engine problem
 [2015-09-03 01:37 UTC] yohgaki@php.net
@untuned20
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

Tl;tr
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] stas@php.net
-Status: Open +Status: Suspended
 [2017-08-05 04:48 UTC] stas@php.net
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 (internals@lists.php.net) as an RFC. Please read the guide about creating RFCs here:
https://wiki.php.net/rfc/howto
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the Internals list (http://php.net/mailing-lists.php) 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-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC