rfc:class_and_interface_name_types

This is an old revision of the document!


PHP RFC: Class and interface name types

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 three new type declarations for use on parameters and return values:

  • classlike, which accepts the name of a class or interface
  • class, which accepts the name of a class
  • interface, which accepts the name of an interface

These type declarations validate values 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); // 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 classlike keyword is now restricted from use as a class, interface or trait name, to avoid conflicting with the type declaration. Breaking backwards-compatibility here is unfortunately necessary in order to introduce a new type declaration. It is the author's opinion that this is unlikely to be a common name in existing code, and therefore should cause few if any backwards-compatibility issues in practice.

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 of the classlike, class and interface declarations 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.1493337928.txt.gz · Last modified: 2017/09/22 13:28 (external edit)