rfc:class-like_primitive_types

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:class-like_primitive_types [2016/12/19 21:18] ajfrfc:class-like_primitive_types [2017/09/22 13:28] (current) – external edit 127.0.0.1
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 an instance of the primitive type object, are also instances of PHP's form of non-primitive, user-defined types: classes.+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 instances of the primitive type object, are also instances of PHP's form of non-primitive, user-defined types: classes.
  
 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, inheritance, and <php>instanceof</php>. 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, inheritance, and <php>instanceof</php>.
Line 53: Line 53:
   * <php>$x instanceof array</php>   * <php>$x instanceof array</php>
   * <php>$x instanceof object</php>   * <php>$x instanceof object</php>
-  * <php>$x instanceof resource</php> (**FIXME:** should I omit this?)+  * <php>$x instanceof resource</php>
  
 ==== Features not extended to primitives ==== ==== Features not extended to primitives ====
Line 62: Line 62:
   * <php>gettype()</php> will continue to report the other primitive types as non-objects   * <php>gettype()</php> will continue to report the other primitive types as non-objects
   * <php>ArrayAccess</php> will not be implemented by <php>array</php>, because it provides mutating methods   * <php>ArrayAccess</php> will not be implemented by <php>array</php>, because it provides mutating methods
 +  * <php>null</php> has no shadow class (see the Open Issues section), but does support <php>instanceof null</php>
  
 **FIXME:** What do I do about reflection? **FIXME:** What do I do about reflection?
 +
 +=== Serializable and resources ===
 +
 +The resource type will not implement the <php>Serializable</php> interface. Though the <php>serialize()</php> function accepts resources, it is broken: they serialise to an integer of the resource ID, and this integer, when deserialised, neither become a resource nor is usable as one. The <php>->deserialize()</php> method would therefore be redundant given resources cannot be deserialised, and the type would not be fulfilling the contract of the interface since resources do not meaningfully/ serialise.
  
 ==== Primitive type class hierarchy ==== ==== Primitive type class hierarchy ====
Line 70: Line 75:
  
 <code php> <code php>
-final class null implements Serializable, JsonSerializable { +/* There is no shadow class for null */ 
-    public function __toString() { /* ... */ } +
-    public function serialize() { /* ... */ } +
-    public function unserialize($serialized) { /* ... */ } +
-    public function jsonSerialize() { /* ... */ } +
-}+
 final class bool implements Serializable, JsonSerializable { final class bool implements Serializable, JsonSerializable {
     public function __toString() { /* ... */ }     public function __toString() { /* ... */ }
Line 110: Line 111:
     }     }
 } }
-final class resource implements Serializable {+final class resource {
     public function __toString() { /* ... */ }     public function __toString() { /* ... */ }
-    public function serialize() { /* ... */ } 
-    public function unserialize($serialized) { /* ... */ } 
 } }
 </code> </code>
Line 132: Line 131:
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
  
-This does not impact existing extensionstheir view of the world is unchanged and the other primitive types are still primitives.+This does not impact existing extensionstheir view of the world is unchanged and the primitive types other than objects are still not objects, internally.
  
 **FIXME:** Reflection. **FIXME:** Reflection.
Line 143: Line 142:
  
 **FIXME:** Reflection, Opcache. **FIXME:** Reflection, Opcache.
 +
 +==== Should null have a shadow class? ====
 +
 +Or in other words, should these features be extended to <php>null</php>?
 +
 +<php>null</php> is a value and type representing the absence of a value. It is a special case among the scalar types, lacking its own type declaration and not being coerced in weak type checking.
 +
 +Would methods like <php>__toString()</php> on <php>null</php> values be more likely to be called in error than intentionally?
 +
 +In JavaScript, ''null'' does not have any properties or methods, but ''true'' and ''false'' do. In contrast, Python's ''None'' (its equivalent to null) //does// have properties and methods, albeit only magic methods.
 +
 +This RFC currently chooses to omit null from the extension of most features of objects to the other types, but excepts <php>instanceof null</php> because it lacks the same potential to create errors.
 +
 +==== 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, <php>instanceof resource</php> could create future backwards-compatibility problems if code uses it to check for values being resources and they later become objects.
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
Line 152: Line 169:
 The extension of these features to the other primitive types opens up a number of future possibilities. The extension of these features to the other primitive types opens up a number of future possibilities.
  
-One of these would be [[http://nikic.github.io/2014/03/14/Methods-on-primitive-types-in-PHP.html|methods on the other primitive types]] (and also properties). This could make string and array manipulation more convenient, and additionally provides an opportunity for a fresh start versus the old standard string and array functions, which have notoriously inconsistent naming and parameter orders.+One of these would be [[http://nikic.github.io/2014/03/14/Methods-on-primitive-types-in-PHP.html|introducing new methods on the other primitive types]] (and also properties). This could lend string and array manipulation the convenience of method calls, and would provide an opportunity for a fresh start versus PHP's existing string and array functions, which have notoriously inconsistent naming and parameter orders
 + 
 +It also means we can easily introduce new superclasses of our other primitive types. For example, a new <php>\Number</php> type superclassing <php>int</php> and <php>float</php>, or a new <php>\Scalar</php> type superclassing everything except objects and arrays. Such superclasses could potentially be extended also by user-defined classes. 
 + 
 +Likewise, we could introduce new interfaces implemented by our other primitive types. One potential use for this is operator overloading. PHP could add an interface for number-like classes, which when implemented, would allow use of the number operators (''+ - * /'' etc.) with objects of that class. That interface could itself be implemented by PHP's own <php>int</php> and <php>float</php> types.
  
-It also means we can easily introduce new superclasses of our other primitive types. For example, a new <php>\Number</php> type superclassing <php>int</php> and <php>float</php>, or a new <php>\Scalar</php> type superclassing everything except objects and arrays.+<php>instanceof</php> could potentially support pseudo-types like <php>callable</php>.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
rfc/class-like_primitive_types.1482182328.txt.gz · Last modified: 2017/09/22 13:28 (external edit)