rfc:null-propagation

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-propagation [2018/01/23 09:17] daverandomrfc:null-propagation [2018/01/29 16:33] (current) – Move introduction to Introduction. The code style change was made to preserve vertical spacing, not because I care about the brace placement holy-war. levim
Line 6: Line 6:
   * First Published at: https://wiki.php.net/rfc/null-propagation   * First Published at: https://wiki.php.net/rfc/null-propagation
  
-===== Proposal =====+===== Introduction =====
 A common requirement in object-oriented programming is the need to check whether an object is ''NULL'' before attempting access its members. At present in PHP this must be done by breaking the statement into multiple parts, typically assigning the result of an expression to a variable and then branching with an ''if'' statement. This RFC proposes an operator that would allow such checks to be written inline. A common requirement in object-oriented programming is the need to check whether an object is ''NULL'' before attempting access its members. At present in PHP this must be done by breaking the statement into multiple parts, typically assigning the result of an expression to a variable and then branching with an ''if'' statement. This RFC proposes an operator that would allow such checks to be written inline.
  
Line 12: Line 12:
  
 <code php> <code php>
-class Foo +class Foo {
-{+
     private $bar;     private $bar;
      
-    public function __construct(?Bar $bar) +    public function __construct(?Bar $bar) {
-    {+
         $this->bar = $bar;         $this->bar = $bar;
     }     }
          
-    public function getBar(): ?Bar +    public function getBar(): ?Bar {
-    {+
         return $this->bar;         return $this->bar;
     }     }
Line 28: Line 25:
      
 $foo = new Foo(null); $foo = new Foo(null);
- 
 $bar = $foo->getBar(); $bar = $foo->getBar();
  
Line 39: Line 35:
 </code> </code>
  
-This proposal would allow a question mark ''?'' to precede any object instance member access ''->''. This would cause the engine to check that the expression on the left of the member access does not evaluate to ''NULL'', if it does then no error will be emitted, the remainder of the expression on the right will be short-circuited and the expression will evaluate to ''NULL'':+===== Proposal ===== 
 + 
 +This proposal allows a question mark ''?'' to precede any object instance member access ''->''. This would cause the engine to check that the expression on the left of the member access does not evaluate to ''NULL'', if it does then no error will be emitted, the remainder of the expression on the right will be short-circuited and the expression will evaluate to ''NULL''. Using the same definitions from the Introduction:
  
 <code php> <code php>
 +$foo = new Foo(null);
 $result = $foo->getBar()?->method(); $result = $foo->getBar()?->method();
 +var_dump($result); // NULL
 </code> </code>
 +
 +This operator does not suppress any errors that are generated by the left hand side of the expression, it only short-circuits the right hand side.
  
 ===== Examples in other languages ===== ===== Examples in other languages =====
 +  * [[https://docs.hhvm.com/hack/operators/null-safe|Hack]]
   * [[https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators|C# and Visual Basic]]   * [[https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators|C# and Visual Basic]]
-  * [[https://github.com/tc39/proposal-optional-chaining|ECMA Script]] (Stage 1 proposal)+  * [[https://github.com/tc39/proposal-optional-chaining|ECMAScript]] (Stage 1 proposal)
   * [[http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/|Ruby]]   * [[http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/|Ruby]]
  
rfc/null-propagation.1516699049.txt.gz · Last modified: 2018/01/23 09:17 by daverandom