I have taken a liking to jQuery/Javascript's way of extending functionality via closures. Is it possible to do something similar in PHP 5.3?
class Foo
{
public $bar;
}
$foo = new Foo;
$foo->bar = function($baz) { echo strtoupper($baz); };
$foo->bar('lorem ipsum dolor sit amet');
// LOREM IPSUM DOLOR SIT AMET
[edit] mixed up 'it' and 'is' in my question. heh.
UPDATE
I downloaded 5.3a3 and it does work!
class Foo
{
protected $bar;
public $baz;
public function __construct($closure)
{
$this->bar = $closure;
}
public function __call($method, $args)
{
$closure = $this->$method;
call_user_func_array($closure, $args);
}
}
$foo = new Foo(function($name) { echo "Hello, $name!\n"; });
$foo->bar('Mon');
// Hello, Mon!
$foo->baz = function($s) { echo strtoupper($s); };
$foo->baz('the quick brown fox jumps over the lazy dog');
// THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG