note 114178 added to functions.anonymous
| From: | jc at jmccc dot com | Date: | Tue, 21 Jan 2014 05:48:46 +0000 |
| Subject: | note 114178 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198291@lists.php.net to get a copy of this message | ||
In 5.3, $this cannot be used in an anonymous function, though you can simply assign $this to another
variable and pass that into the function in the use section, This quick and dirty hack works for at
least calling public functions and accessing public variables of the parent class.
<?php
class Example {
public $blah;
function foo() {
$that = $this;
$tmp = function() use ($that) {
return $that->blah;
}
return $tmp;
}
}
$test = new Example();
$example->blah = "whatever";
echo $example->foo(); // prints "whatever"
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/114178
Del: integrated -- https://master.php.net/note/delete/114178/integrated
Del: useless -- https://master.php.net/note/delete/114178/useless
Del: bad code -- https://master.php.net/note/delete/114178/bad+code
Del: spam -- https://master.php.net/note/delete/114178/spam
Del: non-english -- https://master.php.net/note/delete/114178/non-english
Del: in docs -- https://master.php.net/note/delete/114178/in+docs
Del: other reasons-- https://master.php.net/note/delete/114178
Reject -- https://master.php.net/note/reject/114178
Search -- https://master.php.net/manage/user-notes.php