note 105634 added to functions.anonymous
| From: | HayleyWatson at osu1 dot php dot net | Date: | Fri, 02 Sep 2011 10:03:30 +0000 |
| Subject: | note 105634 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182899@lists.php.net to get a copy of this message | ||
As already noted in the documentation, the 5.4 alpha releases reintroduce $this back into anonymous
functions; note that it is bound early and refers to the object scope in which the function created
it (in contrast with, say, JavaScript, where it refers to the scope in which the function is
called).
So
<?php
class test1
{
private $foo = "This is Class One";
public function make_anonymous_function()
{
return function()
{
echo $this->foo;
};
}
}
class test2
{
private $foo = "This is Class Two";
public function use_anonymous_function()
{
$t1 = new test1;
$func = $t1->make_anonymous_function();
echo $func();
}
}
$t2 = new test2;
$t2->use_anonymous_function();
?>
outputs "This is Class One".
----
Server IP: 69.147.83.197
Probable Submitter: 125.237.147.185
----
Manual Page -- http://www.php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/105634
Del: integrated -- https://master.php.net/note/delete/105634/integrated
Del: useless -- https://master.php.net/note/delete/105634/useless
Del: bad code -- https://master.php.net/note/delete/105634/bad+code
Del: spam -- https://master.php.net/note/delete/105634/spam
Del: non-english -- https://master.php.net/note/delete/105634/non-english
Del: in docs -- https://master.php.net/note/delete/105634/in+docs
Del: other reasons-- https://master.php.net/note/delete/105634
Reject -- https://master.php.net/note/reject/105634
Search -- https://master.php.net/manage/user-notes.php