I’m trying to dump elements of an object’s private property through an anonymous function – of course I could achieve this in any number of other ways, but this highlights a PHP conundrum I can’t solve off the top of my head, short of $foo = $this and using $foo – but THAT won’t give me the private stuff, so… suggestions ?
Sample code:
class MyClass
{
private $payload = Array( 'a' => 'A element', 'b' => 'B element');
static $csvOrder = Array('b','a');
public function toCSV(){
$values = array_map(
function($name) use ($this) { return $this->payload[$name]; },
self::$csvOrder
);
return implode(',',$values);
}
}
$mc = new MyClass();
print $mc->toCSV();
I believe there is absolutely no way to do directly what you propose.
However, you can work around it either by making the anonymous method a class method (this is not what you asked for, but it could be a practical solution) or pulling everything you need out of
$thisexplicitly and passing the extracted values into the function: