php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54311 Implicit variable scope declaration
Submitted: 2011-03-19 04:14 UTC Modified: 2012-03-05 06:18 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dan dot lugg at gmail dot com Assigned:
Status: Wont fix Package: *Programming Data Structures
PHP Version: Irrelevant OS: Windows 7 x64
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-03-19 04:14 UTC] dan dot lugg at gmail dot com
Description:
------------
Feature request for 'use implicit', or semantically similar syntax. Anonymous functions with the use implicit directive inherit all variables from parent scope in which they are declared, not called;

$foo = 'hello';
call_user_func(function() use implicit{
    echo $foo; // hello
});


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-19 18:14 UTC] dan dot lugg at gmail dot com
Further examples exhibiting scope inheritance:

class Test{
    public $func;
    public function callFunc(){
        $this->func();
    }
}

$foo = 'hello';

$test = new Test;
$test->func = function() use implicit{
    echo $foo;
}

$test->callFunc(); // hello
 [2012-03-05 06:18 UTC] rasmus@php.net
I really prefer to be explicit about what you bring into a restricted scope. That 
has been PHP's approach from day 1 with the explicit nature of the 'global' 
keyword to declare explicitly which global variables a function will muck with.

And in PHP 5.4 with the implicit binding of $this I think most of the motivation 
for a request like this is gone anyway.
 [2012-03-05 06:18 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 [2012-03-05 07:23 UTC] dan dot lugg at gmail dot com
Thank you for your response Rasmus.

I can understand the motivation to restrict "scope inheritance", especially with 
the introduction of closure rebinding (which I must say, I am excited to use in 
production)

Hopefully closure rebinding will afford me the functionality that was sought 
after 
with this request. All the same, thank you for your consideration.

:)
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Sep 21 15:01:27 2019 UTC