rfc:class_and_interface_name_types

This is an old revision of the document!


PHP RFC: Class name type

Introduction

There are various cases where the fully-qualified name of a class or interface may be returned from or passed as an argument to a function. While PHP has a type declaration for function names, callable, it does not yet have such a type declaration for class or interface names.

Proposal

This RFC proposes the introduction of a new type declaration for use on parameters and return values, the class type declaration. It would accept the fully-qualified name of a class or interface.

It would validate its value using the following steps:

  1. If the value is not a string, reject it.
  2. If there is no currently-loaded class, interface, or trait with the name given by the value, attempt to autoload it.
  3. If there is now no currently-loaded class or interface (as appropriate) with the name given by the value, reject the value.
  4. Otherwise, accept the value.

A simple example of usage:

<?php
 
function accepts_class(class $class) {
    var_dump($class);
}
 
accepts_class(stdClass::class);    // works
accepts_class("stdClass");         // also works
accepts_class(ArrayAccess::class); // interfaces are also accepted
accepts_class("nonexistant");      // Fatal error: Uncaught TypeError: Argument 1 passed to accepts_class() must be of the type class, string given…

Background and Rationale

FIXME: Talk about Hack, and what it does differently?

FIXME: Justify “classlike”/class/interface distinction?

Backward Incompatible Changes

The class keyword is already a reserved word, so there is no backwards-compatibility break created by its reuse here.

Proposed PHP Version(s)

This is proposed for the next PHP 7.x, currently PHP 7.2.

RFC Impact

To Opcache

Testing revealed that Opcache's type inference needed updating to accommodate the new type declaration. This has been done and it seems to work, but it is possible other areas of incompatibility exist and have been missed.

Unaffected PHP Functionality

Classes, interfaces and traits continue not to be objects in themselves, and are only referenced by name.

Future Scope

A supertype could be introduced, which could name not only classes and interfaces, but also primitive PHP types (int, string etc.) and possibly pseudo-types.

The potential future ability to use variables containing type names in place of literal type names in source code could facilitate generic programming.

Proposed Voting Choices

This would require a 2/3 majority, as a language change. The vote would be a Yes/No vote as to whether to accept the RFC for the appropriate future version of PHP.

Patches and Tests

There is a patch here which contains tests: https://github.com/php/php-src/compare/master...hikari-no-yume:class_type_declaration

It does not yet handle inheritance concerns.

There is not yet a pull request.

There is not yet a patch for the language specification.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

FIXME: Links to external references, discussions or RFCs

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