mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-21 19:16:23 +00:00
603 B
603 B
date | id | title |
---|---|---|
2020-11-10 | d07536b9-8d42-4b60-92a5-0348acb0a3db | PHP nullsafe operator |
Description
Introduced in PHP 8.0, nullsafe operators1
enable you to safely use methods that may return null
.
Syntax
class bar
{
public function excellent(): string
{
return "excellent";
}
}
class foo
{
public function getBarOrNull(): ?bar
{
return rand(0, 1) === 1 ? new bar() : null;
}
}
$foo = new foo();
echo $foo->getBarOrNull()?->excellent();