-1
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.

rurumita
  • 17
  • 2
  • in short take a look at the [documents](https://www.php.net/manual/en/migration71.new-features.php) – meewog Apr 04 '22 at 06:39

2 Answers2

0

"?" 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

ikyuchukov
  • 565
  • 3
  • 12
0

This is Nullable type.

PHP Documentation