#45604 [Opn->Fbk]: Lambda functions and $this scope

From: Date: Wed, 23 Jul 2008 13:48:21 +0000
Subject: #45604 [Opn->Fbk]: Lambda functions and $this scope
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-127194@lists.php.net to get a copy of this message
ID: 45604 Updated by: jani@php.net Reported By: george at noodles dot gr -Status: Open +Status: Feedback Bug Type: Scripting Engine problem Operating System: * PHP Version: 5.3CVS-2008-07-23 (snap) New Comment: Please report this kind of things to internals@lists.php.net as this feature is still under heavy development and jury is out to decide such things. Previous Comments: ------------------------------------------------------------------------ [2008-07-23 12:07:06] george at noodles dot gr Description: ------------ When creating a normal function inside a class and calling it, the function doesn't have access to $this. "Fatal error: Using $this when not in object context" When creating a lambda function inside a class, $this is visible from the new function's scope and can be accessed normally. Wouldn't it be better (and maybe safer) to allow the use of $this as a closure instead of passing it to the new lambda function? Currently trying to use $this as a closure dies with a "Fatal error: Cannot use $this as lexical variable" error. Example for the suggestion. $x = function () use ($this) { return $this->hello; }; Reproduce code: --------------- <?php class something { public $hello = 'Hello world!'; public function world() { $x = function () { return $this->hello; }; return $x(); } } $s = new something(); echo $s->world(); ?> Expected result: ---------------- Not to be able to use $this. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45604&edit=1

« previous php.bugs (#127194) next »