'What does question mark do in php? [duplicate]
class Service
{
private Logger $logger;
public function __construct(
?Logger $logger = null,
) {
$this->logger = $logger ?? new NullLogger();
}
}
I'm learning php oop.
What does question mark do in ?Logger $logger = null??
I googled but couldn't find an answer.
Solution 1:[1]
"?" in front of argument or property means that this argument/property is nullabe, in your example you can pass either a Logger object or null.
You can read more about it here: https://www.php.net/manual/en/migration71.new-features.php
Solution 2:[2]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ikyuchukov |
| Solution 2 |
