rfc:class-like_primitive_types
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:class-like_primitive_types [2016/12/07 05:11] – incomplete draft ajf | rfc:class-like_primitive_types [2016/12/19 22:00] – ajf | ||
---|---|---|---|
Line 8: | Line 8: | ||
===== Background ===== | ===== Background ===== | ||
- | PHP has a small set of built-in types: null, Boolean, integer, float, string, array, object and resource. These types are known as “primitive” types, because they are built-in to the language. Of these, objects are special. Objects, themselves | + | PHP has a small set of built-in types of value: null, Boolean, integer, float, string, array, object and resource. These types are known as “primitive” types, because they are built-in to the language. Of these, objects are special. Objects, themselves |
- | Classes and objects have evolved considerably since their introduction in PHP 3.0. As part of this evolution, they have gained several features that are exclusive to them, which the other primitive types consequently cannot make use of. These features include instance and static methods, properties, class constants, interface implementation, | + | Since their introduction in PHP 3.0, classes and objects |
- | This set of exclusive features creates an awkward divide between PHP's objects and it' | + | This set of exclusive features creates an awkward divide between PHP's objects and its other primitive types. Various tasks must be done in different ways for objects versus other types, creating the need for workarounds to bridge the gap. For example, the [[rfc: |
- | ===== Introduction ===== | + | By contrast, some of PHP's contemporaries, |
- | The elevator pitch for the RFC. The first paragraph | + | |
+ | There could be benefits to adopting | ||
+ | |||
+ | Hence, this RFC suggests | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | All the features and examples of the proposal. | ||
- | To [[http://news.php.net/php.internals/66051|paraphrase Zeev Suraski]], explain hows the proposal brings substantial value to be considered | + | This RFC proposes to extend PHP's object-oriented features to the other primitive types. They would not become objects, and their internal representation would be unchanged. However, they would now benefit from most of the features of objects. |
- | for inclusion in one of the world' | + | |
+ | ==== Features extended to primitives ==== | ||
+ | |||
+ | The following features of objects would now be extended to the other primitive types. | ||
+ | |||
+ | === Interfaces and inheritance === | ||
+ | |||
+ | These types would now implement PHP's pre-defined interfaces as appropriate (see the class hierarchy further down for a list of which). | ||
+ | |||
+ | These types would not be extendible by user classes, but could in future potentially extend new built-in abstract classes (e.g. < | ||
+ | |||
+ | Type declarations and < | ||
+ | |||
+ | === Instance methods and properties === | ||
+ | |||
+ | These types will provide instance methods as necessary to conform to PHP's pre-defined interfaces (see the class hierarchy further down for a list of which). | ||
+ | |||
+ | In future, they could provide other methods, or magic instance properties (see the future scope section). | ||
+ | |||
+ | === instanceof === | ||
+ | |||
+ | The < | ||
+ | |||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | |||
+ | ==== Features not extended | ||
+ | |||
+ | The following features would **not** now be extended to the other primitive types: | ||
+ | |||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | |||
+ | **FIXME:** What do I do about reflection? | ||
+ | |||
+ | === Serializable and resources === | ||
+ | |||
+ | The resource type will not implement the < | ||
+ | |||
+ | ==== Primitive type class hierarchy ==== | ||
+ | |||
+ | Behind each other primitive type, there would now be a hidden internal class, or //shadow class//. Attempts to call methods, look up properties, use < | ||
- | Remember that the RFC contents should be easily reusable in the PHP Documentation. | + | <code php> |
+ | /* There is no shadow class for null */ | ||
- | If applicable, you may wish to use the language specification as a reference. | + | final class bool implements Serializable, JsonSerializable { |
+ | public function __toString() { /* ... */ } | ||
+ | public function serialize() { /* ... */ } | ||
+ | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
+ | } | ||
+ | final class int implements Serializable, | ||
+ | public function __toString() { /* ... */ } | ||
+ | public function serialize() { /* ... */ } | ||
+ | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
+ | } | ||
+ | final class float implements Serializable, | ||
+ | public function __toString() { /* ... */ } | ||
+ | public function serialize() { /* ... */ } | ||
+ | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
+ | } | ||
+ | final class string implements Serializable, | ||
+ | public function __toString() { /* ... */ } | ||
+ | public function serialize() { /* ... */ } | ||
+ | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
+ | } | ||
+ | final class array implements Serializable, | ||
+ | public function __toString() { /* ... */ } | ||
+ | public function serialize() { /* ... */ } | ||
+ | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
+ | public function count() { /* ... */ } | ||
+ | public function getIterator() { | ||
+ | return new ArrayIterator($this); | ||
+ | } | ||
+ | } | ||
+ | final class resource { | ||
+ | public function __toString() { /* ... */ } | ||
+ | } | ||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | What breaks, and what is the justification | + | |
+ | In order to support the < | ||
+ | |||
+ | **FIXME** | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | List the proposed | + | |
+ | This is proposed | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
==== To SAPIs ==== | ==== To SAPIs ==== | ||
- | Describe the impact | + | |
+ | This has no particular special | ||
==== To Existing Extensions ==== | ==== To Existing Extensions ==== | ||
- | Will existing extensions be affected? | ||
- | ==== To Opcache ==== | + | This does not impact existing extensions: their view of the world is unchanged and the primitive types other than objects are still not objects, internally. |
- | It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP. | + | |
- | Please explain how you have verified your RFC's compatibility with opcache. | + | **FIXME:** Reflection. |
- | ==== New Constants | + | ==== To Opcache |
- | Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation. | + | |
- | ==== php.ini Defaults ==== | + | **FIXME**. |
- | If there are any php.ini settings then list: | + | |
- | | + | |
- | | + | |
- | | + | |
===== Open Issues ===== | ===== Open Issues ===== | ||
- | Make sure there are no open issues when the vote starts! | + | |
+ | **FIXME:** Reflection, Opcache. | ||
+ | |||
+ | ==== Should null have a shadow class? ==== | ||
+ | |||
+ | Or in other words, should these features be extended to < | ||
+ | |||
+ | < | ||
+ | |||
+ | Would methods like < | ||
+ | |||
+ | In JavaScript, '' | ||
+ | |||
+ | This RFC currently chooses to omit null from support by most of these features. | ||
+ | |||
+ | ==== Should resource be supported? ==== | ||
+ | |||
+ | Resource is a legacy type that could be wholly replaced by objects in future. Extending these features to this type would be further entrenching it and contrary to the goal of its eventual removal. | ||
+ | |||
+ | Beyond that, supporting < | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
- | List existing areas/ | ||
- | This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. | + | **FIXME** |
===== Future Scope ===== | ===== Future Scope ===== | ||
- | This sections details areas where the feature might be improved | + | |
+ | The extension of these features to the other primitive types opens up a number of future possibilities. | ||
+ | |||
+ | One of these would be [[http:// | ||
+ | |||
+ | It also means we can easily introduce new superclasses of our other primitive types. For example, a new < | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Include these so readers know where you are heading and can discuss the proposed voting options. | ||
- | State whether this project requires | + | This is a major language change, so it would require |
+ | |||
+ | It would be a Yes/No vote on whether to accept the RFC. | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | Links to any external patches and tests go here. | ||
- | If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. | + | There is no interpreter |
- | Make it clear if the patch is intended to be the final patch, or is just a prototype. | + | There is no language |
- | + | ||
- | For changes affecting the core language, you should also provide a patch for the language specification. | + | |
===== Implementation ===== | ===== Implementation ===== | ||
Line 87: | Line 195: | ||
===== References ===== | ===== References ===== | ||
- | Links to external references, discussions or RFCs | + | |
+ | **FIXME** | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- | Keep this updated with features that were discussed on the mail lists. | + | |
+ | None yet. |
rfc/class-like_primitive_types.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1