Your $.fn.myplugin
function is very similar to the __call()
magic function in PHP. However, you would have to define it in a class and emulate the logic:
class Example {
private $methods;
public function __construct() {
$methods = array();
$methods['init'] = function() {};
$methods['hello'] = function() {};
}
public function __call($name, $arguments) {
if( isset( $methods[$name])) {
call_user_func_array( $methods[$name], $arguments);
} else if( $arguments[0] instanceof Closure) {
// We were passed an anonymous function, I actually don't think this is possible, you'd have to pass it in as an argument
call_user_func_array( $methods['init'], $arguments);
} else {
throw new Exception( "Method " . $name . " does not exist");
}
}
}
Then, you would do:
$obj = new Example();
$obj->hello();
It's not tested but hopefully it's a start.