Permalink
Browse files

Implement ??= operator

RFC: https://wiki.php.net/rfc/null_coalesce_equal_operator

$a ??= $b is $a ?? ($a = $b), with the difference that $a is only
evaluated once, to the degree that this is possible. In particular
in $a[foo()] ?? $b function foo() is only ever called once.
However, the variable access themselves will be reevaluated.
  • Loading branch information...
nikic committed Jan 15, 2019
1 parent 50ddff9 commit a50198d0fef652ca052cda642d6e98a9101eb73f
@@ -90,6 +90,15 @@ PHP 7.4 UPGRADE NOTES
This will enforce that $user->id can only be assigned integer and
$user->name can only be assigned strings. For more information see the
RFC: https://wiki.php.net/rfc/typed_properties_v2
. Added support for coalesce assign (??=) operator. For example:

$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
$array['key'] = computeDefault();
}

RFC: https://wiki.php.net/rfc/null_coalesce_equal_operator

- FFI:
. A new extension which provides a simple way to call native functions, access
@@ -0,0 +1,127 @@
--TEST--
Coalesce assign (??=): Basic behavior
--FILE--
<?php
// Refcounted values
$foo = "fo";
$foo .= "o";
$bar = "ba";
$bar .= "r";
// Identity function used to track single-evaluation
function id($arg) {
echo "id($arg)\n";
return $arg;
}
echo "Simple variables:\n";
$a = 123;
$a ??= 456;
var_dump($a);
$b = null;
$b ??= $foo;
var_dump($b);
$c = $foo;
$c ??= $bar;
var_dump($c);
$d ??= $foo;
var_dump($c);
echo "\nArrays:\n";
$ary = [];
$ary["foo"] ??= 123;
$ary[$foo] ??= $bar;
$ary[$bar] ??= $foo;
var_dump($ary);
$ary = [];
$ary[id($foo)] ??= 123;
$ary[id($foo)] ??= $bar;
$ary[id($bar)] ??= $foo;
var_dump($ary);
echo "\nObjects:\n";
$obj = new stdClass;
$obj->foo ??= 123;
$obj->$foo ??= $bar;
$obj->$bar ??= $foo;
var_dump($obj);
$obj = new stdClass;
$obj->{id($foo)} ??= 123;
$obj->{id($foo)} ??= $bar;
$obj->{id($bar)} ??= $foo;
var_dump($obj);
class Test {
public static $foo;
public static $bar;
}
echo "\nStatic props:\n";
Test::$foo ??= 123;
Test::$$foo ??= $bar;
Test::$$bar ??= $foo;
var_dump(Test::$foo, Test::$bar);
Test::$foo = null;
Test::$bar = null;
Test::${id($foo)} ??= 123;
Test::${id($foo)} ??= $bar;
Test::${id($bar)} ??= $foo;
var_dump(Test::$foo, Test::$bar);
?>
--EXPECT--
Simple variables:
int(123)
string(3) "foo"
string(3) "foo"
string(3) "foo"

Arrays:
array(2) {
["foo"]=>
int(123)
["bar"]=>
string(3) "foo"
}
id(foo)
id(foo)
id(bar)
array(2) {
["foo"]=>
int(123)
["bar"]=>
string(3) "foo"
}

Objects:
object(stdClass)#1 (2) {
["foo"]=>
int(123)
["bar"]=>
string(3) "foo"
}
id(foo)
id(foo)
id(bar)
object(stdClass)#2 (2) {
["foo"]=>
int(123)
["bar"]=>
string(3) "foo"
}

Static props:
int(123)
string(3) "foo"
id(foo)
id(foo)
id(bar)
int(123)
string(3) "foo"
@@ -0,0 +1,84 @@
--TEST--
Coalesce assign (??=): Exception handling
--FILE--
<?php
$foo = "fo";
$foo .= "o";
$bar = "ba";
$bar .= "r";
function id($arg) {
echo "id($arg)\n";
return $arg;
}
function do_throw($msg) {
throw new Exception($msg);
}
$ary = [];
try {
$ary[id($foo)] ??= do_throw("ex1");
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
var_dump($ary);
class AA implements ArrayAccess {
public function offsetExists($k) {
return true;
}
public function &offsetGet($k) {
$var = ["foo" => "bar"];
return $var;
}
public function offsetSet($k,$v) {}
public function offsetUnset($k) {}
}
class Dtor {
public function __destruct() {
throw new Exception("dtor");
}
}
$ary = new AA;
try {
$ary[new Dtor][id($foo)] ??= $bar;
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
var_dump($foo);
class AA2 implements ArrayAccess {
public function offsetExists($k) {
return false;
}
public function offsetGet($k) {
return null;
}
public function offsetSet($k,$v) {}
public function offsetUnset($k) {}
}
$ary = ["foo" => new AA2];
try {
$ary[id($foo)][new Dtor] ??= $bar;
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
var_dump($foo);
?>
--EXPECT--
id(foo)
ex1
array(0) {
}
id(foo)
dtor
string(3) "foo"
id(foo)
dtor
string(3) "foo"
@@ -0,0 +1,70 @@
--TEST--
Coalesce assign (??=): ArrayAccess handling
--FILE--
<?php
function id($arg) {
echo "id($arg)\n";
return $arg;
}
class AA implements ArrayAccess {
public $data;
public function __construct($data = []) {
$this->data = $data;
}
public function &offsetGet($k) {
echo "offsetGet($k)\n";
return $this->data[$k];
}
public function offsetExists($k) {
echo "offsetExists($k)\n";
return array_key_exists($k, $this->data);
}
public function offsetSet($k,$v) {
echo "offsetSet($k,$v)\n";
$this->data[$k] = $v;
}
public function offsetUnset($k) { }
}
$ary = new AA(["foo" => new AA, "null" => null]);
echo "[foo]\n";
$ary["foo"] ??= "bar";
echo "[bar]\n";
$ary["bar"] ??= "foo";
echo "[null]\n";
$ary["null"] ??= "baz";
echo "[foo][bar]\n";
$ary["foo"]["bar"] ??= "abc";
echo "[foo][bar]\n";
$ary["foo"]["bar"] ??= "def";
?>
--EXPECT--
[foo]
offsetExists(foo)
offsetGet(foo)
[bar]
offsetExists(bar)
offsetSet(bar,foo)
[null]
offsetExists(null)
offsetGet(null)
offsetSet(null,baz)
[foo][bar]
offsetExists(foo)
offsetGet(foo)
offsetExists(bar)
offsetGet(foo)
offsetSet(bar,abc)
[foo][bar]
offsetExists(foo)
offsetGet(foo)
offsetExists(bar)
offsetGet(bar)
@@ -0,0 +1,11 @@
--TEST--
Coalesce assign (??=): Non-writable variable
--FILE--
<?php
function foo() { return 123; }
foo() ??= 456;
?>
--EXPECTF--
Fatal error: Can't use function return value in write context in %s on line %d
@@ -0,0 +1,14 @@
--TEST--
Coalesce assign (??=): Cannot reassign $this
--FILE--
<?php
class Test {
public function foobar() {
$this ??= 123;
}
}
?>
--EXPECTF--
Fatal error: Cannot re-assign $this in %s on line %d
@@ -1672,6 +1672,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
EMPTY_SWITCH_DEFAULT_CASE();
}
break;
case ZEND_AST_ASSIGN_COALESCE: BINARY_OP(" \?\?= ", 90, 91, 90);
case ZEND_AST_BINARY_OP:
switch (ast->attr) {
case ZEND_ADD: BINARY_OP(" + ", 200, 200, 201);
@@ -120,6 +120,7 @@ enum _zend_ast_kind {
ZEND_AST_INSTANCEOF,
ZEND_AST_YIELD,
ZEND_AST_COALESCE,
ZEND_AST_ASSIGN_COALESCE,

ZEND_AST_STATIC,
ZEND_AST_WHILE,
Oops, something went wrong.

0 comments on commit a50198d

Please sign in to comment.