note 105887 added to functions.anonymous
| From: | neyronius at gmail dot com | Date: | Fri, 23 Sep 2011 05:08:09 +0000 |
| Subject: | note 105887 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183363@lists.php.net to get a copy of this message | ||
Using $this within closure
<?php
class Test
{
public $a = 2;
public $lambda = null;
public function setLambda()
{
$_this = $this; //To prevent error
$this->lambda = function() use ($_this)
{
echo $_this->a; //only public properties and methods
};
}
}
$t = new Test;
$t->setLambda();
$lambda = $t->lambda; // It's wrong to call lambda as $t->lambda();
$lambda(); //output: 2
?>
----
Server IP: 69.147.83.197
Probable Submitter: 80.68.12.85
----
Manual Page -- http://www.php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/105887
Del: integrated -- https://master.php.net/note/delete/105887/integrated
Del: useless -- https://master.php.net/note/delete/105887/useless
Del: bad code -- https://master.php.net/note/delete/105887/bad+code
Del: spam -- https://master.php.net/note/delete/105887/spam
Del: non-english -- https://master.php.net/note/delete/105887/non-english
Del: in docs -- https://master.php.net/note/delete/105887/in+docs
Del: other reasons-- https://master.php.net/note/delete/105887
Reject -- https://master.php.net/note/reject/105887
Search -- https://master.php.net/manage/user-notes.php