rfc:property_type_hints
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:property_type_hints [2015/10/17 15:14] – describe property-initialization mindplay | rfc:property_type_hints [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Property type-hints ====== | ====== PHP RFC: Property type-hints ====== | ||
- | * Version: | + | * Version: 1.0 |
* Date: 2015-07-19 | * Date: 2015-07-19 | ||
* Author: Rasmus Schultz < | * Author: Rasmus Schultz < | ||
* Status: Draft | * Status: Draft | ||
- | * First Published at: http:// | + | * First Published at: http:// |
===== Introduction ===== | ===== Introduction ===== | ||
Line 13: | Line 13: | ||
The significance of type-checked properties is a given, as demonstrated by it's inclusion in Hack, as well as other recent scripting languages, including Typescript, Dart and ActionScript. The need for type-hinting is demonstrated by the widespread use of php-doc, and support for such type-hinting in modern PHP IDEs. | The significance of type-checked properties is a given, as demonstrated by it's inclusion in Hack, as well as other recent scripting languages, including Typescript, Dart and ActionScript. The need for type-hinting is demonstrated by the widespread use of php-doc, and support for such type-hinting in modern PHP IDEs. | ||
- | The proposed syntax is compatible with that of Hack, and is a natural addition to the language, resembling the syntax set forth by plenty | + | The proposed syntax is compatible with that of Hack, and is a natural addition to the language, resembling the syntax set forth by other gruadually-typed (and statically-typed) languages. |
===== Proposal ===== | ===== Proposal ===== | ||
Line 24: | Line 24: | ||
</ | </ | ||
- | This declares a public property $value which can only be initialized or set with an integer | + | This declares a public property |
- | Scalar type hints, as per the addition in PHP 7, are permitted, as well as any class or interface name. | + | Scalar type hints (e.g. int, float, bool, string |
+ | |||
+ | The precise behavior as far as type-checking and type-conversions is dependent on the [[https:// | ||
Violating the type-check when initializing or setting a property, will result in a catchable fatal error: | Violating the type-check when initializing or setting a property, will result in a catchable fatal error: | ||
Line 48: | Line 50: | ||
</ | </ | ||
- | This behavior guarantees the integrity of a type-checked property. | + | This behavior guarantees the integrity of a type-checked property, even if modified indirectly. |
==== Property Initialization ==== | ==== Property Initialization ==== | ||
Line 62: | Line 64: | ||
</ | </ | ||
- | Note that, while some languages would generate a parse error for the declaration itself, such behavior would not be practical in PHP, where, for example, initialization with a scalar expression such as '' | + | Note that, while some languages would generate a parse error for the declaration itself, such behavior would not be practical in PHP, where, for example, initialization with a scalar expression such as '' |
+ | |||
+ | Also note that the proposed behavior is consistent with initialization of constants, in the sense that e.g. '' | ||
==== ReflectionProperty ==== | ==== ReflectionProperty ==== | ||
Line 75: | Line 79: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Note that type-checking (and/or scalar type-conversion, | ||
TBD: [[https:// | TBD: [[https:// | ||
+ | |||
+ | ===== Non-features ===== | ||
+ | |||
+ | The inclusion of a '' | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 87: | Line 97: | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
The impact on opcache needs to be examined. | The impact on opcache needs to be examined. | ||
+ | |||
+ | ==== To Zend Engine ==== | ||
+ | Static type-hints could enable some engine optimizations - this should be investigated. | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | TBD: Documentation for existing, standard classes (reflection, | + | None |
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
The introduction of an optional type-hint does not affect legacy PHP code. | The introduction of an optional type-hint does not affect legacy PHP code. | ||
+ | |||
+ | To preserve backwards compatibility, | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
Line 110: | Line 125: | ||
===== References ===== | ===== References ===== | ||
- | TBD | + | None |
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- | TBD | + | None |
rfc/property_type_hints.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1