#47690 [NEW]: $this not accessible inside a lambda function declared within a class
| From: | crocodile2u at gmail dot com | Date: | Tue, 17 Mar 2009 14:01:14 +0000 |
| Subject: | #47690 [NEW]: $this not accessible inside a lambda function declared within a class | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-134811@lists.php.net to get a copy of this message | ||
From: crocodile2u at gmail dot com
Operating system: ubuntu 8.10
PHP version: 5.3CVS-2009-03-17 (snap)
PHP Bug Type: Scripting Engine problem
Bug description: $this not accessible inside a lambda function declared within a class
Description:
------------
http://wiki.php.net/rfc/closures in the
"Interaction with OOP" section, it
is said that $this is accessible from within a lambda-function that is
declared in a method. This is not true for php-5.3.0beta.
Reproduce code:
---------------
<?php
// code completely taken from http://wiki.php.net/rfc/closures
class Example {
private $search;
public function __construct ($search) {
$this->search = $search;
}
public function setSearch ($search) {
$this->search = $search;
}
public function getReplacer ($replacement) {
return function ($text) use ($replacement) {
return str_replace ($this->search, $replacement, $text);
};
}
}
$example = new Example ('hello');
$replacer = $example->getReplacer ('goodbye');
echo $replacer ('hello world'); // goodbye world
$example->setSearch ('world');
echo $replacer ('hello world'); // hello goodbye
Expected result:
----------------
no errors occur
Actual result:
--------------
Fatal error: Using $this when not in object context in
/home/vbolshov/tmp/x.php on line 16
--
Edit bug report at http://bugs.php.net/?id=47690&edit=1
--
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=47690&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=47690&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=47690&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=47690&r=fixedcvs
Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=47690&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=47690&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=47690&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=47690&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=47690&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=47690&r=support
Expected behavior: http://bugs.php.net/fix.php?id=47690&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=47690&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=47690&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=47690&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=47690&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=47690&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=47690&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=47690&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=47690&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=47690&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=47690&r=mysqlcfg