#47690 [Bgs]: $this not accessible inside a lambda function declared within a class
| From: | scottmac@php.net | Date: | Tue, 17 Mar 2009 14:59:22 +0000 |
| Subject: | #47690 [Bgs]: $this not accessible inside a lambda function declared within a class | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-134818@lists.php.net to get a copy of this message | ||
ID: 47690
Updated by: scottmac@php.net
Reported By: crocodile2u at gmail dot com
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: ubuntu 8.10
PHP Version: 5.3CVS-2009-03-17 (snap)
New Comment:
I already did it when I saw this report.
Previous Comments:
------------------------------------------------------------------------
[2009-03-17 14:24:49] crocodile2u at gmail dot com
Ok, Thanks for a quick reply.
I would suggest adding that link to the wiki article. (I have just
registered in the wiki but there is no "Edit this page" button there for
me, guess it is a priviledge that I do not have).
------------------------------------------------------------------------
[2009-03-17 14:15:24] scottmac@php.net
See http://wiki.php.net/rfc/closures/removal-of-this
------------------------------------------------------------------------
[2009-03-17 14:01:14] crocodile2u at gmail dot com
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 this bug report at http://bugs.php.net/?id=47690&edit=1