rfc:object-typehint

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:object-typehint [2016/11/14 09:56] brzuchalrfc:object-typehint [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== PHP RFC: Object typehint ====== ====== PHP RFC: Object typehint ======
-  * Version: 0.1+  * Version: 1.1
   * Date: 2016-08-12   * Date: 2016-08-12
   * Author: Michał Brzuchalski <michal.brzuchalski@gmail.com>, Dan Ackroyd <danack@php.net>   * Author: Michał Brzuchalski <michal.brzuchalski@gmail.com>, Dan Ackroyd <danack@php.net>
-  * Status: On Hold+  * Status: Implemented (in PHP 7.2)
   * First Published at: https://wiki.php.net/rfc/object-typehint   * First Published at: https://wiki.php.net/rfc/object-typehint
  
Line 20: Line 20:
 As it would be used internally, object would become a reserved classname, unavailable for use as a class name in userland code. As it would be used internally, object would become a reserved classname, unavailable for use as a class name in userland code.
  
-For class methods that use object as either a parameter or return type.+For class methods that use object as either a parameter or return type, the inheritance checks would use contravariance for parameter types, and covariance for return types. 
 ===== Examples ===== ===== Examples =====
  
Line 112: Line 113:
     switch ($type) {     switch ($type) {
         case 'foo': { return new Foo(); }         case 'foo': { return new Foo(); }
-        case 'bar': { return new Ba(); }+        case 'bar': { return new Bar(); }
         case 'zot': { new zot(); }  // Ooops, this is an error         case 'zot': { new zot(); }  // Ooops, this is an error
     }     }
Line 188: Line 189:
 === Argument typehint contravariance === === Argument typehint contravariance ===
  
- +Classes that extend another class, or implement an interface may broaden a parameter type from a specific class, to more generic 'object' typehint.
-Classes extending/implementing may broaden method arguments with class name type using more generic 'object' typehint+
-This behavior is the similar as that proposed for 'iterable' and behaves contravariant.+
  
 <code php> <code php>
Line 209: Line 208:
 </code> </code>
  
-In above example 'Foo' typehint was broaden to 'object' type in extending class.+This is normally known as [[https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Contravariant_method_argument_type|contravariant]]. 
 + 
 +Classes extending/implementing may not narrow method arguments types from 'object' to more specific type
 + 
 +<code php> 
 +class Foo { 
 +
 + 
 +class Bar { 
 +    public function foo(object $object) : object { 
 +        return $object; 
 +    } 
 +
 + 
 +class Baz extends Bar { 
 +    public function foo(Foo $object) : object { 
 +        return $object;     
 +    } 
 +
 +</code> 
 + 
 +In this situation the standard PHP warning for incompatible signatures will be raised. 
 + 
 +<code php> 
 +Declaration of Baz::foo(Foo $object): object should be compatible with Bar::foo(object $object): object 
 +</code>
  
 === Return type covariance === === Return type covariance ===
  
 Classes extending/implementing may narrow methods return types from 'object' to specified class name. Classes extending/implementing may narrow methods return types from 'object' to specified class name.
-This behavior is the similar as that proposed for 'iterable' and behaves covariant.+This behavior is the similar as that proposed for 'iterable' and is normally called [[https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Covariant_method_return_type|covariant]].
  
 <code php> <code php>
Line 244: Line 268:
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-Althought object is soft reserved word this RFC adds object as reserved classname. This may collide with existing classes which are neither namespaced nor prefixed.+Although 'objectis already a soft reserved wordthis RFC adds object as fully reserved classname.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 262: Line 286:
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-As this is a language change, a 2/3 majority is required. The vote is a straight Yes/No vote for accepting the RFC and merging the patch.+Voting will run from 2017-05-17, for two weeks, until 2017-05-31. 
 +As this is a language change, a 2/3 majority is required. 
 +The vote is a straight Yes/No vote for accepting the RFC and merging the patch
 +The additional vote is also a straight Yes/No vote for accepting variance behaviour on the object type.
  
-<doodle title="Accept the object typehint RFC with covariance patch for PHP 7.2?" auth="brzuchal" voteType="single" closed="true">+<doodle title="Accepting the object typehint RFC for PHP 7.2?" auth="brzuchal" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +<doodle title="Object type should implement variance?" auth="brzuchal" voteType="single" closed="true">
    * Yes    * Yes
    * No    * No
Line 271: Line 303:
 ===== Patches and Tests ===== ===== Patches and Tests =====
   * PR[[https://github.com/php/php-src/pull/2080|#2080]]   * PR[[https://github.com/php/php-src/pull/2080|#2080]]
 +  * Online Runtime Execution at [[https://3v4l.org/|3v4l.org]] on tab "RFC Branches"
 +
 +===== Discussions =====
 +
 +  * [[https://externals.io/thread/514|[RFC][DISCUSSION] Object type hint, now with added variance]]
 +  * [[https://externals.io/thread/412|[RFC][DISCUSSION] Object type hint]]
 +
rfc/object-typehint.1479117366.txt.gz · Last modified: 2017/09/22 13:28 (external edit)