Closure Type Hint with __invoke
I've seen that type hinting with `__invoke` interfaces works as return values. But then quickly realized, that it doesn't work on the input side.
Is it possible to fix that?
Example:
interface ClosureInterface
{
public function __invoke(string $string, int $int): ?bool;
}
function acceptClosure(ClosureInterface $closure)
{
}
// Doesn't work:
// Expected ClosureInterface, got Closure
acceptClosure(function ($string, $int) {
return true;
});
function returnClosure(): ClosureInterface
{
// Doesn't work:
// Return value is expected to be 'ClosureInterface', '\Closure' returned
return function ($string, $int) {
return true;
};
}
// This works, it typehints: 'string : string, int : int'
returnClosure()('string', 1);
请先登录再写评论。
Please use PhpDocs for that:
With this change you just allow any Closure and you don't get any type hint for the given parameters or the return type.