note 114178 deleted from functions.anonymous by cmb
| From: | cmb@php.net | Date: | Sun, 11 Aug 2019 11:27:54 +0000 |
| Subject: | note 114178 deleted from functions.anonymous by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211841@lists.php.net to get a copy of this message | ||
Note Submitter: jc at jmccc dot com
----
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"