For years I’ve been biting bullets attempting to write functional code in PHP with hacks like this:
class Foo {
function addOne($wu) {
return $wu + 1;
}
function getBiggerThings(array $things) {
$that = $this;
return array_map(function ($i) use ($that) {
return $that->addOne($i);
}, $things);
}
Today a colleague pointed out that I could write:
return array_map(array($this, 'addOne'), $things);
I can’t find any documentation on this on php.net. Am I reading the callback type documentation incorrectly?
I’d like to highlight the following from commented PHP docs:
This actually is since a pretty long time, so to say before PHP 5.3 which allowed you the anonymous function “workaround”.