rfc:generics

This is an old revision of the document!


PHP RFC: Introduce generics into PHP

Introduction

Many languages, including hack by now, support generics, which for instance allow to create and typehint for specialized collections without the need to implement such a collection for every single entity. This RFC proposes the introduction of generics into PHP. Adding generic support to existing collections, e.g. in SPL, may be part of another RFC.

Proposal

A class is considered generic when it has one or more types defined. The defined types can be used in both method parameter type hinting and return type hinting.

class Entry<KeyType, ValueType>
{
    protected $key;
    protected $value;
 
    public function __construct(KeyType $key, ValueType $value)
    {
        $this->key   = $key;
        $this->value = $value;
    }
 
    public function getKey(): KeyType
    {
        return $this->key;
    }
 
    public function getValue(): ValueType
    {
        return $this->value;
    }
}
 
$entry = new Entry<int, string>(1, 'test'); // Valid
$entry = new Entry(1, 'test'); // Throws an EngineException about missing types

Functions and methods are able to type hint against specific variations of a given class:

function foo(Entry<int, string> $entry)
{
    // …
}
 
foo(new Entry<int, string>(1, 'test')); // Valid
foo(new Entry<int, int>(1, 2)); // Throws an EngineException about invalid argument

A generic class can also force a type to extend a specific class or implement a specific interface:

class ClassA<Foo : \Bar>
{
}

Backward Incompatible Changes

No BC breaks are expected from this proposal.

Proposed PHP Version(s)

This proposal aims for PHP 7.1.

Proposed Voting Choices

For this proposal to be accepted, a 2/3 majority is required.

Patches and Tests

No patch has been written for this yet. As I'm not a C-coder myself, I encourage others to write a patch based on this proposal.

References

rfc/generics.1441050765.txt.gz · Last modified: 2017/09/22 13:28 (external edit)