JQuery Style PHP Closure in version 5.3


#!/usr/bin/php
<?php
class Document {
private $body_closure = NULL;
protected $reflection = NULL;

public function __construct($function)
{
if ( ! $function instanceOf Closure)
throw new InvalidArgumentException();

$this->body_closure = $function;
$this->reflection = new ReflectionFunction($function);
}

public function __invoke()
{
$args = func_get_args();
return $this->reflection->invokeArgs($args);
}

public function getClosure()
{
return $this->body_closure;
}

public static function ready($function){
if($function instanceOf Closure){
static $instance;
if(!is_object($instance)){
$class_name = __class__;
$instance = new $class_name($function);
}
$instance();
}
else{
print "Invalid body closure!\n";
}
}
};

Document::ready(
function(){
print "hello\n";
}
);