Table of Contents

PHP RFC: Structural Typing for Closures

Introduction

This RFC proposes to introduce structural typing for closures when used with interfaces containing only an __invoke() method. This feature aims to simplify the interaction between closures and single-method interfaces, making it easier to work with them in a more consistent and expressive manner.

Proposal

Given an interface with only an __invoke() method, if a parameter, return, property, or instanceof operation is typed against that interface, and a closure is passed or returned as a value, the engine checks the signature of the closure to ensure it satisfies the contract defined by the interface. This check would also happen when using is_subclass_of() and is_a().

This would encourage developers to define more explicit and expressive type declarations, which can lead to better code maintainability and fewer runtime errors.

Example

interface TwoInts
{
    public function __invoke(int $a, int $b): int;
}
 
function takeTwo(TwoInts $c)
{
    // This is a TwoInts anon class object at this point
    $c(1, 2);
}
 
// This works
takeTwo(fn(int $x, int $y): int => $x + $y);
 
// This throws a TypeError
bad(fn(int $x, float $y, string $z) => whatever);

Backward Incompatible Changes

The proposed change does not introduce any backward incompatibilities. Existing code will continue to work as it does currently. The structural typing of closures would be an optional feature that developers can choose to use when it makes sense for their specific use case.

Open Questions

Future Scope

Proposed Voting Choices

This RFC proposes a straight Yes/No vote. A two-thirds majority is required for acceptance.