rfc:nullable_typehints
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:nullable_typehints [2014/05/03 17:47] – Grammar. levim | rfc:nullable_typehints [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Declaring Nullable Types ====== | + | This RFC has moved to [[https:// |
- | * Version: 0.1 | + | |
- | * Date: 2014-04-10 | + | |
- | * Author: Levi Morrison < | + | |
- | * Status: Draft | + | |
- | * First Published at: https:// | + | |
- | + | ||
- | ===== Introduction ===== | + | |
- | This RFC proposes new syntax to allow type declarations to also accept < | + | |
- | + | ||
- | Consider the function: | + | |
- | + | ||
- | < | + | |
- | function foo(DateTime $time, $a) {} | + | |
- | </ | + | |
- | + | ||
- | In this situation you are not allowed to pass < | + | |
- | + | ||
- | ===== Proposal ===== | + | |
- | This proposal adds a marker ''?'' | + | |
- | + | ||
- | The ''?'' | + | |
- | < | + | |
- | function f(?callable $p, $a) {} // valid | + | |
- | </ | + | |
- | + | ||
- | Note that nullable parameters are required; you cannot omit the parameter. | + | |
- | + | ||
- | ==== Inheritance in parameters ==== | + | |
- | + | ||
- | The ''?'' | + | |
- | + | ||
- | < | + | |
- | // Valid use: loosening the nullable marker in a parameter: | + | |
- | interface Fooable { | + | |
- | function foo(Fooable $f); | + | |
- | } | + | |
- | interface Foo extends Fooable { | + | |
- | function foo(? | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | // Invalid use: tightening the nullable marker in a parameter: | + | |
- | interface Fooable { | + | |
- | function foo(? | + | |
- | } | + | |
- | interface Foo extends Fooable { | + | |
- | function foo(Fooable $f); | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ==== Return Types ==== | + | |
- | If the [[rfc: | + | |
- | + | ||
- | Here's an example of a binary tree class that uses the ''?'' | + | |
- | + | ||
- | < | + | |
- | // Possible usage of ? modifier for both parameters and return types | + | |
- | class BinaryTree { | + | |
- | public $value; | + | |
- | private $right; | + | |
- | private $left; | + | |
- | + | ||
- | function right(): ?BinaryTree { | + | |
- | return $this-> | + | |
- | } | + | |
- | function left(): ?BinaryTree { | + | |
- | return $this-> | + | |
- | } | + | |
- | + | ||
- | function setRight(? | + | |
- | $this-> | + | |
- | } | + | |
- | function setLeft(? | + | |
- | $this-> | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Note that it doesn' | + | |
- | + | ||
- | === Inheritance in Return Types === | + | |
- | + | ||
- | In return types the ''?'' | + | |
- | < | + | |
- | interface Fooable { | + | |
- | function foo(): ?Fooable; | + | |
- | } | + | |
- | interface StrictFooable extends Fooable { | + | |
- | function foo(): Fooable; // valid | + | |
- | } | + | |
- | </ | + | |
- | < | + | |
- | interface Fooable { | + | |
- | function foo(): Fooable; | + | |
- | } | + | |
- | interface LooseFooable extends Fooable { | + | |
- | function foo(): ?Fooable; // invalid | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ==== Differences from Default Parameters ==== | + | |
- | The null marker does not have a default; it does not default to < | + | |
- | < | + | |
- | function f(?callable $p) {} | + | |
- | f(); // invalid; function f does not have a default | + | |
- | </ | + | |
- | + | ||
- | ===== Proposed PHP Version(s) ===== | + | |
- | This RFC targets 5.7 or 6.0, whichever comes first. | + | |
- | + | ||
- | ===== RFC Impact ===== | + | |
- | + | ||
- | ==== To Backward Incompatibility ==== | + | |
- | This RFC maintains backwards compatibility. This RFC does not deprecate the default value syntax. | + | |
- | + | ||
- | ==== To SAPIs ==== | + | |
- | This RFC has no impact to SAPIs. | + | |
- | + | ||
- | ==== To Existing Extensions ==== | + | |
- | TODO | + | |
- | + | ||
- | ==== New Constants ==== | + | |
- | This RFC does not propose any new constants or ini defaults. | + | |
- | + | ||
- | ===== Open Issues ===== | + | |
- | Make sure there are no open issues when the vote starts! | + | |
- | + | ||
- | ===== Unaffected PHP Functionality ===== | + | |
- | This RFC does not deprecate the default value syntax. While there is some overlap of features between it and this RFC, they serve different purposes. As such, the default value syntax will remain. | + | |
- | + | ||
- | ===== Future Scope ===== | + | |
- | TODO | + | |
- | + | ||
- | ===== Proposed Voting Choices ===== | + | |
- | This RFC modifies the PHP language syntax and therefore requires a two-third majority of votes. | + | |
- | + | ||
- | ===== Patches and Tests ===== | + | |
- | There is currently no patch. | + | |
- | + | ||
- | ===== Implementation ===== | + | |
- | TODO | + | |
- | + | ||
- | After the project is implemented, | + | |
- | - the version(s) it was merged to | + | |
- | - a link to the git commit(s) | + | |
- | - a link to the PHP manual entry for the feature | + | |
- | + | ||
- | ===== References ===== | + | |
- | TODO | + |
rfc/nullable_typehints.1399139260.txt.gz · Last modified: 2017/09/22 13:28 (external edit)