Closures are the part of 5.3 I reached for first, and the first thing I did was put one inside a method. Inside the closure body the object is simply not there: the function has no object context, and referring to the current instance is a fatal error rather than a notice, so it takes the whole request down with it.
class OrderReport
{
private $rate;
public function withTax(array $lines)
{
$rate = $this->rate; // copy it out first
return array_map(function ($line) use ($rate) {
return $line['net'] * (1 + $rate);
}, $lines);
}
}
Importing what the closure needs with use is the whole workaround, and it copies at the point the closure is defined rather than where it runs — changing the variable afterwards does not change what the closure sees, unless it was imported by reference with use (&$rate). There is no way to hand a 5.3 closure an object context after the fact. When the closure turns out to need three or four members, that is the language pointing at a small class with __invoke(), or at a plain private method and a callable array.