Req #69571 [Opn->Wfx]: type hinting not possible in use clause of closures
| From: | requinix@php.net | Date: | Mon, 04 May 2015 19:45:09 +0000 |
| Subject: | Req #69571 [Opn->Wfx]: type hinting not possible in use clause of closures | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192480@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69571&edit=1
ID: 69571
Updated by: requinix@php.net
Reported by: thomas+php at koch dot ro
Summary: type hinting not possible in use clause of closures
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: *General Issues
PHP Version: 5.6.8
Block user comment: N
Private report: N
New Comment:
Type hinting doesn't make sense for a use clause. In there is supposed to be a list of
variables that exist in the current scope that get copied into the closure*. As such the code should
know the type (likely even the actual value) of each variable. Compare that with a parameter list
where the function doesn't know what values someone may try to pass into it, thus typing
becomes useful.
On that note, your example. It doesn't make sense either because $b does not exist, and if you
run the code (without the stdClass hint) then you'll get a warning about that. If you then
resolve the warning by defining $b then, well, you know exactly what $b is.
* References are a little different in that the function gets a copy of the reference, not of the
underlying value. And like in other places, a by-ref variable doesn't have to exist at the time
of the call.
Previous Comments:
------------------------------------------------------------------------
[2015-05-04 15:07:17] thomas+php at koch dot ro
Description:
------------
I would have expected that I can use the same type hinting in the 'use' clause of a
closure as in the parameter list. It's not a bug since it's not documented to be working
but it's an issue in consistency.
Test script:
---------------
<?php
$a = function() use ( stdClass $b) { return 'hi';};
echo $a();
Expected result:
----------------
hi
Actual result:
--------------
Parse error: syntax error, unexpected 'stdClass' (T_STRING), expecting '&'
or variable (T_VARIABLE) in [...] on line 2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69571&edit=1