From 1f3bd4f8913033243c75affc02b07283b9da5306 Mon Sep 17 00:00:00 2001 From: Max Zhuk Date: Fri, 20 Aug 2021 14:33:02 +0300 Subject: [PATCH] 7.0: Null coalescing operator and Anonymous classes --- src/User.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/User.php b/src/User.php index 2e1baf6..ea60afd 100644 --- a/src/User.php +++ b/src/User.php @@ -26,7 +26,7 @@ class User */ public function __construct(string $name, $city, $birthday = null) { - $this->name = isset($name) ? $name : ''; + $this->name = $name ?? ''; $this->city = $city; $this->birthday = $birthday; } @@ -74,7 +74,7 @@ public function getRole() public function setRole(string $role = self::CUSTOMER) { - $this->role = isset($this->role) ? $this->role : $role; + $this->role = $this->role ?? $role; } public function getAvatar(): string @@ -91,8 +91,16 @@ public function getAvatar(): string } } - public function setLogger(LoggerInterface $logger) + /** + * @param LoggerInterface|null $logger + */ + public function setLogger($logger = null) { - $this->logger = $logger; + $this->logger = $logger ?? new class implements LoggerInterface { + public function log($message) + { + echo $message; + } + }; } }