1
0
mirror of https://github.com/php/php-src.git synced 2026-04-12 10:33:11 +02:00
Files
archived-php-src/Zend/tests/nullsafe_operator/014.phpt
Ilija Tovilo 9bf119832d Implement nullsafe ?-> operator
RFC: https://wiki.php.net/rfc/nullsafe_operator

Closes GH-5619.

Co-authored-by: Nikita Popov <nikita.ppv@gmail.com>
2020-07-24 10:05:03 +02:00

48 lines
922 B
PHP

--TEST--
Test nullsafe in binary op
--FILE--
<?php
function try_and_dump($fn) {
try {
var_dump($fn());
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
}
class Foo {
public function bar() {
echo "bar\n";
}
}
$foo = new Foo();
$null = null;
try_and_dump(fn() => $null?->null() + $null?->null());
try_and_dump(fn() => $foo?->bar() + $null?->null());
try_and_dump(fn() => $null?->null() + $foo?->bar());
try_and_dump(fn() => $foo->bar() + $null?->null());
try_and_dump(fn() => $null?->null() + $foo->bar());
try_and_dump(fn() => $null?->null() + $null->null());
try_and_dump(fn() => $null->null() + $null?->null());
try_and_dump(fn() => ($foo?->bar() + $foo?->bar())?->baz());
?>
--EXPECT--
int(0)
bar
int(0)
bar
int(0)
bar
int(0)
bar
int(0)
Call to a member function null() on null
Call to a member function null() on null
bar
bar
Call to a member function baz() on int