Skip to content

Instantly share code, notes, and snippets.

@azjezz
Last active February 11, 2019 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azjezz/2cf2e0330adbbe6886167408fc96b799 to your computer and use it in GitHub Desktop.
Save azjezz/2cf2e0330adbbe6886167408fc96b799 to your computer and use it in GitHub Desktop.
Polyfill for `call_user_func` and `call_user_func_array` in hack lang
<?hh // strict
namespace Polyfill;
use type ReflectionMethod;
use type ReflectionFunction;
function call_user_func(mixed $callable, mixed ...$args): mixed {
return call_user_func_array($callable, $args);
}
function call_user_func_array(mixed $callable, varray<mixed> $args): mixed {
if (\is_object($callable)) {
$reflection = new ReflectionMethod($callable, '__invoke');
return $reflection->invokeArgs($callable, $args);
} elseif ($callable is string) {
$reflection = new ReflectionFunction($callable);
return $reflection->invokeArgs($args);
} elseif ($callable is (string, string)) {
$reflection = new ReflectionMethod($callable[0], $callable[1]);
return $reflection->invokeArgs(null, $args);
} elseif ($callable is (mixed, string)) {
$reflection = new ReflectionMethod($callable[0], $callable[1]);
return $reflection->invokeArgs($callable[0], $args);
} else {
$reflection = new ReflectionFunction($callable);
return $reflection->invokeArgs($args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment