Callables syntactically have few ways of passing them around.
This RFC is supposed to introduce unified short way of creating closures from callables
and reduce callable type hint checks at runtime by using closures.
Currently there are few ways of declaring callables and they differ while dereferencing function or method. Assuming given example we'll gonan discuss all available 6 ways passing callable.
namespace Util {
function writeln(string $message) : void {
echo $message . \PHP_EOL;
}
class Terminal {
public static function println(string $message) : void {
echo $message . \PHP_EOL;
}
public function writeln(string $message) : void {
echo $message . \PHP_EOL;
}
}
class Console extends Terminal {
public function __invoke(string $message) : void {
echo $message . \PHP_EOL;
}
}
}
namespace {
function helloWorld(callable $callable) : void {
$callable('Hello World!');
}
}Simple callback are possible to pass using their name.
helloWorld('Util\\writeln');
call_user_func('Util\writeln', 'Hello World!');Static class method call first way is quite similar and requires method name after '::'.
helloWorld('Util\\Terminal::println');
call_user_func('Util\\Terminal::println'), 'Hello World!');Static class method call can be also passed using array construct with class name at 0 index and method name at 1 index.
helloWorld([Util\Terminal::class, 'println']);
call_user_func([Util\\Terminal::class, 'println']), 'Hello World!');Object method call is passed using array construct with two params - which is object instance variable and method name.
$terminal = new \Util\Terminal();
helloWorld([$terminal, 'writeln']);
call_user_func([$terminal, 'writeln'], 'Hello World!');Relative static class method call is passed using array withtwo params - which is class name and method name with base class name delimited by '::'.
$console = new Util\Console();
call_user_func([Util\Console::class, 'parent::writeln'], 'Hello World!');Objects implementing __invoke can be used as callables using instance variable.
$console = new Util\Console();
helloWorld($console);
call_user_func($console, 'Hello World!');- Syntax is based on passing function names as a string or array with instance and method name as a string etc. which is very hard to refactor using for eg. IDE or detecting using regular expressions.
- Passed callables are different type variables, like: 'string', 'array' or specified object instance.
- Passed callables may not be valid callables which is validated at call time and can pass 'callable' type hint without warning.
The way to unify passing callables is creating closures using same syntax as invoking functions without their argument list and parenthesis around braces and call them all the same way, which benefits are short closure from callable syntax which shortens creation of closures for most of cases.
Short closure from callable function would look like:
$writeln = {Util\writeln};
// is a simplification for
$writeln = Closure::fromCallable('Util\writeln');Static class method short closure that would look like:
$println = {Util\Terminal::println};
// instead of
$println = Closure::fromCallable([Util\Terminal::class, 'writeln']);
// and
$println = Closure::fromCallable('Util\Terminal::writeln');Object instance method short closure that would look like:
$writeln = {$terminal->writeln};
// instead of
$writeln = Closure::fromCallable([$terminal, 'writeln']);Invokable objects short closure that would be:
$console = new Util\Console();
$callback = {$console};
// instead of
$callback = Closure::fromCallable($console);- Every callable closure is valid for all the time when it's passed around.
- No need to validate
callabletypehint against given argument every time. - Common syntax for most of callable types.
- Syntax similar to function/method call.
- No need to pass function/method/class names as strings which is IDE friendly for refactor.
- Easy to parse by static analysis tools because of consistent syntax.
For those who are interested, during a quick discussion amongst our team this morning, the other proposed syntaxes were:
array_map($this->someMethod, $array);orarray_map($this->someMethod::method, $array);