rfc:class-like_primitive_types
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:class-like_primitive_types [2016/12/18 17:06] – / ajf | rfc:class-like_primitive_types [2016/12/19 22:04] – ajf | ||
---|---|---|---|
Line 8: | Line 8: | ||
===== Background ===== | ===== Background ===== | ||
- | 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 | + | 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 |
Since their introduction in PHP 3.0, classes and objects 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 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, | ||
Line 24: | Line 24: | ||
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. | 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. | ||
- | ==== Features ==== | + | ==== Features |
- | The following features of objects would now be extended to the other primitive types: | + | The following features of objects would now be extended to the other primitive types. |
- | * Interfaces | + | === Interfaces |
- | * Inheritance - At present this would go unused, but in future it would be possible | + | |
- | * < | + | These types would now implement PHP's pre-defined interfaces as appropriate (see the class hierarchy further down for a list of which). |
- | * Type declaration checks for interfaces and inheritance - As with instanceof | + | |
- | * Instance methods - These types will provide instance methods as necessary to conform to PHP's pre-defined interfaces (<php>-> | + | These types would not be extendible by user classes, but could in future potentially extend new built-in abstract classes (e.g. <php>int</ |
- | * Instance properties - At present none would be added, and the types would not support adding undefined properties, but these types could provide magic properties in future (e.g. <php>->length</ | + | |
+ | Type declarations and < | ||
+ | |||
+ | === Instance methods and properties === | ||
+ | |||
+ | These types will provide instance methods as necessary | ||
+ | |||
+ | In future, they could provide other methods, or magic instance properties (see the future scope section). | ||
+ | |||
+ | === instanceof === | ||
+ | |||
+ | The <php>instanceof</ | ||
+ | |||
+ | * <php>$x instanceof null</ | ||
+ | * <php>$x instanceof bool</ | ||
+ | * <php>$x instanceof | ||
+ | * <php>$x instanceof float</ | ||
+ | * < | ||
+ | * < | ||
+ | * <php>$x instanceof object</ | ||
+ | * <php>$x instanceof resource</ | ||
+ | |||
+ | ==== Features not extended to primitives ==== | ||
The following features would **not** now be extended to the other primitive types: | The following features would **not** now be extended to the other primitive types: | ||
Line 40: | Line 62: | ||
* < | * < | ||
* < | * < | ||
+ | * < | ||
**FIXME:** What do I do about reflection? | **FIXME:** What do I do about reflection? | ||
+ | |||
+ | === Serializable and resources === | ||
+ | |||
+ | The resource type will not implement the < | ||
==== Primitive type class hierarchy ==== | ==== Primitive type class hierarchy ==== | ||
- | Behind each other primitive | + | Behind each other primitive |
<code php> | <code php> | ||
- | final class null implements Serializable { | + | /* There is no shadow |
+ | |||
+ | final class bool implements Serializable, JsonSerializable | ||
public function __toString() { /* ... */ } | public function __toString() { /* ... */ } | ||
- | public function __debugInfo() { /* ... */ } | ||
public function serialize() { /* ... */ } | public function serialize() { /* ... */ } | ||
public function unserialize($serialized) { /* ... */ } | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
} | } | ||
- | final class bool implements Serializable { | + | final class int implements Serializable, JsonSerializable |
public function __toString() { /* ... */ } | public function __toString() { /* ... */ } | ||
- | public function __debugInfo() { /* ... */ } | ||
public function serialize() { /* ... */ } | public function serialize() { /* ... */ } | ||
public function unserialize($serialized) { /* ... */ } | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
} | } | ||
- | final class int implements Serializable { | + | final class float implements Serializable, JsonSerializable |
public function __toString() { /* ... */ } | public function __toString() { /* ... */ } | ||
- | public function __debugInfo() { /* ... */ } | ||
public function serialize() { /* ... */ } | public function serialize() { /* ... */ } | ||
public function unserialize($serialized) { /* ... */ } | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
} | } | ||
- | final class float implements Serializable { | + | final class string |
public function __toString() { /* ... */ } | public function __toString() { /* ... */ } | ||
- | public function __debugInfo() { /* ... */ } | ||
public function serialize() { /* ... */ } | public function serialize() { /* ... */ } | ||
public function unserialize($serialized) { /* ... */ } | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
} | } | ||
- | final class string | + | final class array implements Serializable, JsonSerializable, |
public function __toString() { /* ... */ } | public function __toString() { /* ... */ } | ||
- | public function __debugInfo() { /* ... */ } | ||
- | public function serialize() { /* ... */ } | ||
- | public function unserialize($serialized) { /* ... */ } | ||
- | } | ||
- | final class array implements Serializable, | ||
- | public function __toString() { /* ... */ } | ||
- | public function __debugInfo() { /* ... */ } | ||
public function serialize() { /* ... */ } | public function serialize() { /* ... */ } | ||
public function unserialize($serialized) { /* ... */ } | public function unserialize($serialized) { /* ... */ } | ||
+ | public function jsonSerialize() { /* ... */ } | ||
public function count() { /* ... */ } | public function count() { /* ... */ } | ||
public function getIterator() { | public function getIterator() { | ||
Line 88: | Line 111: | ||
} | } | ||
} | } | ||
- | final class resource | + | final class resource { |
public function __toString() { /* ... */ } | public function __toString() { /* ... */ } | ||
- | public function __debugInfo() { /* ... */ } | ||
- | public function serialize() { /* ... */ } | ||
- | public function unserialize($serialized) { /* ... */ } | ||
} | } | ||
</ | </ | ||
Line 111: | Line 131: | ||
==== To Existing Extensions ==== | ==== To Existing Extensions ==== | ||
- | This does not impact existing extensions, their view of the world is unchanged and the other primitive types are still primitives. | + | This does not impact existing extensions: their view of the world is unchanged and the primitive types other than objects |
**FIXME:** Reflection. | **FIXME:** Reflection. | ||
Line 122: | Line 142: | ||
**FIXME:** Reflection, Opcache. | **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. | ||
+ | |||
+ | In particular, < | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== |
rfc/class-like_primitive_types.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1