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
Last revisionBoth sides next revision
rfc:null-false-standalone-types [2022/03/10 16:45] – Clear up Reflection caveat girgiasrfc:null-false-standalone-types [2022/03/27 11:10] – Close vote 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: In Voting
   * 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/pull/7546]]
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 =====
rfc/null-false-standalone-types.txt · Last modified: 2022/04/08 16:26 by girgias