php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49543 closures cannot import/inherit $this
Submitted: 2009-09-13 14:55 UTC Modified: 2011-06-13 06:15 UTC
Votes:23
Avg. Score:4.8 ± 0.5
Reproduced:23 of 23 (100.0%)
Same Version:9 (39.1%)
Same OS:5 (21.7%)
From: mjs at beebo dot org Assigned: jani (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.0 OS: Ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mjs at beebo dot org
New email:
PHP Version: OS:

 

 [2009-09-13 14:55 UTC] mjs at beebo dot org
Description:
------------
Attempting to import/inherit $this produces the compile-time error 
"Cannot use $this as lexical variable."

Note that the workaround of assigning $this to the temporary variable 
$tmp, and inheriting $tmp instead does work, so there appears to be no 
limitation in the engine:

class Foo {

    public function bar() {

        $tmp = $this;
        return function() use ($tmp) {
            echo "in closure\n";
        }

    }

}

See also http://wiki.php.net/rfc/closures/removal-of-this.  (It 
appears that $this was once automatically imported into the closure's 
scope, but that this turned out to be a bad idea.  This bug report 
concerns what happens when $this is explicitly imported, however.)

Reproduce code:
---------------
<?php

class Foo {

    public function bar() {

        return function() use ($this) {
            echo "in closure\n";
        }

    }

}



Expected result:
----------------
in closure

Actual result:
--------------
PHP Fatal error:  Cannot use $this as lexical variable in 
/mnt/hgfs/workspace/scratch/wart2.php on line 7


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-11 19:17 UTC] martin dot partel at gmail dot com
The '$tmp' workaround is not good enough, since private/protected members cannot be accessed that way. This renders closures frustratingly useless in OO code in quite a few situations.

All other OO languages with closures that I know of allow such access one way or the other. Come on guys, find your consensus and get it done!
 [2010-12-17 12:18 UTC] jani@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: Scripting Engine problem -Assigned To: +Assigned To: jani
 [2010-12-17 12:18 UTC] jani@php.net
This was added in trunk (will not be in 5.3.x) but whatever trunk becomes.
 [2011-06-13 04:28 UTC] php at hotblocks dot nl
When can we expect this? Go PHP mixins!
 [2011-06-13 06:15 UTC] rasmus@php.net
It is in 5.4
 [2013-03-01 10:42 UTC] netmosfera at email dot it
would be nice also to have a way to alias variables when importing into the 
closure's scope

which becomes very useful for

function() use ($this as $x) { };
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 05:01:29 2024 UTC