Saturday, December 1, 2012

invoking methods on php's stdClass

I figured out a way to add methods and call them on the stdClass object, using anonymous functions:

$obj = new stdClass();
$anon = function ($value) { return "My value is $value." ; } ;

$obj->value = "a";
$obj->method = $anon;

$method = $obj->method;
echo $method($obj->value) . "\n";