Edit report at http://bugs.php.net/bug.php?id=49543&edit=1

 ID:               49543
 Comment by:       martin dot partel at gmail dot com
 Reported by:      mjs at beebo dot org
 Summary:          closures cannot import/inherit $this
 Status:           Open
 Type:             Feature/Change Request
 Package:          Feature/Change Request
 Operating System: Ubuntu
 PHP Version:      5.3.0

 New Comment:

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!


Previous Comments:
------------------------------------------------------------------------
[2009-09-13 14:55:50] 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




------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=49543&edit=1

Reply via email to