#45604 [NEW]: Lambda functions and $this - scope
| From: | george at noodles dot gr | Date: | Wed, 23 Jul 2008 12:07:06 +0000 |
| Subject: | #45604 [NEW]: Lambda functions and $this - scope | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-127186@lists.php.net to get a copy of this message | ||
From: george at noodles dot gr
Operating system: Windows
PHP version: 5.3CVS-2008-07-23 (snap)
PHP Bug Type: *General Issues
Bug description: Lambda functions and $this - scope
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 bug report at http://bugs.php.net/?id=45604&edit=1
--
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=45604&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=45604&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=45604&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=45604&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=45604&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=45604&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=45604&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=45604&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=45604&r=support
Expected behavior: http://bugs.php.net/fix.php?id=45604&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=45604&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=45604&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=45604&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=45604&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=45604&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=45604&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=45604&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=45604&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=45604&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=45604&r=mysqlcfg