rfc:class_and_interface_name_types
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:class_and_interface_name_types [2017/04/28 00:05] – created ajf | rfc:class_and_interface_name_types [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Class and interface | + | ====== PHP RFC: Class name type ====== |
* Version: 0.1 | * Version: 0.1 | ||
- | * Date: 2017-04-28 | + | * Date: 2017-04-29 |
* Author: Andrea Faulds, ajf@ajf.me | * Author: Andrea Faulds, ajf@ajf.me | ||
* Status: Draft | * Status: Draft | ||
Line 10: | Line 10: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes the introduction of three new type declarations | + | This RFC proposes the introduction of a new type declaration |
- | * '' | + | It would validate |
- | * < | + | |
- | * < | + | |
- | + | ||
- | These type declarations | + | |
- If the value is not a string, reject it. | - If the value is not a string, reject it. | ||
- If there is no currently-loaded class, interface, or trait with the name given by the value, attempt to autoload it. | - If there is no currently-loaded class, interface, or trait with the name given by the value, attempt to autoload it. | ||
- | - If there is now no currently-loaded class or interface | + | - If there is now no currently-loaded class or interface with the name given by the value, reject the value. |
- Otherwise, accept the value. | - Otherwise, accept the value. | ||
+ | |||
+ | As a parameter type, it would permit default values of < | ||
A simple example of usage: | A simple example of usage: | ||
Line 28: | Line 26: | ||
<?php | <?php | ||
- | function accepts_class(class $class) { | + | function accepts_class(class $class = stdClass::class) { |
var_dump($class); | var_dump($class); | ||
} | } | ||
- | accepts_class(stdClass:: | + | accepts_class(); |
- | accepts_class(" | + | accepts_class(stdClass:: |
- | accepts_class(ArrayAccess:: | + | accepts_class(" |
+ | accepts_class(ArrayAccess:: | ||
+ | accepts_class(" | ||
</ | </ | ||
- | ===== Background and Rationale ===== | + | ===== Rationale |
+ | |||
+ | ==== Naming: " | ||
+ | |||
+ | This RFC currently proposes to call this type declaration < | ||
+ | |||
+ | The name < | ||
+ | |||
+ | * It is short | ||
+ | * It is already a reserved word, so introducing it requires no backwards-incompatibility break | ||
+ | * It resembles the < | ||
+ | * It avoids the redundancy of saying " | ||
+ | * It aligns with how PHP treats a class and its fully-qualified name the same in several places (e.g. < | ||
+ | |||
+ | However, there are also arguments in favour of '' | ||
+ | |||
+ | * It conveys more directly that the value is a string containing a class name | ||
+ | * It is already in use by Hack, so using this would prevent further divergence between Hack and PHP | ||
+ | |||
+ | ==== Should " | ||
+ | |||
+ | This RFC currently proposes that there would be a single new type, < | ||
+ | |||
+ | Having < | ||
+ | |||
+ | * It limits the number of new types that are introduced (one versus two or three) | ||
+ | * It aligns with the internal '' | ||
+ | * It aligns with how < | ||
+ | * Interfaces are essentially glorified abstract classes | ||
+ | * PHP internally represents interfaces as a kind of class | ||
+ | * Reflection represents interfaces as a kind of class | ||
+ | * The type declaration need not be perfectly fine-grained, | ||
+ | |||
+ | That being said, there are also arguments in favour of < | ||
+ | |||
+ | * Class and interface definitions use different keywords | ||
+ | * < | ||
+ | * It might be surprising to see the name of an // | ||
+ | |||
+ | There is an advantage to a separate < | ||
+ | |||
+ | If there was a separate < | ||
- | FIXME: Talk about Hack, and what it does differently? | + | If none of these approaches are palatable, there could be a //third// type accepting both classes |
- | FIXME: Justify “classlike”/ | + | All the possibilities in this section can and have been implemented provisionally, |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | The '' | + | The < |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 57: | Line 98: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | A supertype | + | A supertype could be introduced, which could name not only classes and interfaces, but also primitive PHP types (< |
The potential future ability to use variables containing type names in place of literal type names in source code could facilitate generic programming. | The potential future ability to use variables containing type names in place of literal type names in source code could facilitate generic programming. | ||
Line 67: | Line 108: | ||
There is a patch here which contains tests: https:// | There is a patch here which contains tests: https:// | ||
- | It does not yet handle inheritance concerns. | + | The commits for versions with multiple new types do not handle inheritance concerns. |
There is not yet a pull request. | There is not yet a pull request. |
rfc/class_and_interface_name_types.1493337928.txt.gz · Last modified: 2017/09/22 13:28 (external edit)