Last active
February 11, 2019 23:28
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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