rfc:null-false-standalone-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:null-false-standalone-types [2022/03/10 16:45] – Clear up Reflection caveat girgiasrfc:null-false-standalone-types [2022/04/08 16:26] (current) – Change status to implemented girgias
Line 4: Line 4:
   * Date: 2022-02-20   * Date: 2022-02-20
   * Author: George Peter Banyard, <girgias@php.net>   * Author: George Peter Banyard, <girgias@php.net>
-  * Status: Under Discussion+  * Status: Implemented
   * Target Version: PHP 8.2   * Target Version: PHP 8.2
-  * Implementation: [[https://github.com/php/php-src/pull/7546]]+  * Implementation: [[https://github.com/php/php-src/commit/6039c07a3afd64a42c9b9f1ed994ca971db67a1e]]
   * First Published at: [[http://wiki.php.net/rfc/null-standalone-type]]   * First Published at: [[http://wiki.php.net/rfc/null-standalone-type]]
  
Line 132: Line 132:
   Allows Null: true   Allows Null: true
 </code> </code>
 +
 +==== Example ====
 +
 +We take an example from unit testing but it can be applied to other more general cases,
 +where a class which implements an interface always returns ''null''.
 +
 +<PHP>
 +class User {}
 +
 +interface UserFinder
 +{
 +    function findUserByEmail(): User|null;
 +}
 +
 +class AlwaysNullUserFinder implements UserFinder
 +{
 +    function findUserByEmail(): null
 +    {
 +        return null;
 +    }
 +}
 +</PHP>
 +
 +Currently it is not possible to write this code in PHP as it gives the error:
 +<blockquote>Fatal error: Null can not be used as a standalone type</blockquote>
 +
 +This means that an incorrect ''User|null'' return definition needs to used for the ''findUserByEmail()'' method.
 +That leads to further confusion as a static analyzer analysing that class will give a 'method can never return type User' error.
 +
 +The same problem exists for interfaces that have a return type that is a union of false with another type.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 144: Line 174:
  
 As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted. As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
 +
 +Voting started on 2022-03-12 and will end on 2022-03-26.
 +<doodle title="Accept Allow null and false as stand-alone types RFC?" auth="girgias" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Implementation ===== ===== Implementation =====
Line 151: Line 187:
 After the project is implemented, this section should contain After the project is implemented, this section should contain
  
-  * the version(s) it was merged into +  * Version 8.2.0 
-  * a link to the git commit(s)+  * Commit: https://github.com/php/php-src/commit/6039c07a3afd64a42c9b9f1ed994ca971db67a1e
   * a link to the PHP manual entry for the feature   * a link to the PHP manual entry for the feature
  
 ===== References ===== ===== References =====
  
rfc/null-false-standalone-types.1646930752.txt.gz · Last modified: 2022/03/10 16:45 by girgias