rfc:null-false-standalone-types

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:null-false-standalone-types [2022/02/20 17:50] – created 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 92: Line 92:
  
 Reflection support is as expected with the notable exception that ''null|false'' will produce a ReflectionUnionType instead of a ReflectionNamedType contrary to other ''null|T'' types. Reflection support is as expected with the notable exception that ''null|false'' will produce a ReflectionUnionType instead of a ReflectionNamedType contrary to other ''null|T'' types.
 +
 +A concrete case of how this looks like:
 +<PHP>
 +<?php
 +function dumpType(ReflectionUnionType $rt) {
 +    echo "Type $rt:\n";
 +    echo "Allows null: " . ($rt->allowsNull() ? "true" : "false") . "\n";
 +    foreach ($rt->getTypes() as $type) {
 +        echo "  Name: " . $type->getName() . "\n";
 +        echo "  String: " . (string) $type . "\n";
 +        echo "  Allows Null: " . ($type->allowsNull() ? "true" : "false") . "\n";
 +    }
 +}
 +
 +function test1(): null|false { }
 +function test2(): ?false { }
 +
 +
 +dumpType((new ReflectionFunction('test1'))->getReturnType());
 +dumpType((new ReflectionFunction('test2'))->getReturnType());
 +</PHP>
 +Will produce the following output:
 +<code>
 +Type false|null:
 +Allows null: true
 +  Name: false
 +  String: false
 +  Allows Null: false
 +  Name: null
 +  String: null
 +  Allows Null: true
 +Type false|null:
 +Allows null: true
 +  Name: false
 +  String: false
 +  Allows Null: false
 +  Name: null
 +  String: null
 +  Allows Null: true
 +</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 104: 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 111: 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.1645379410.txt.gz · Last modified: 2022/02/20 17:50 by girgias