Last active
March 22, 2021 05:52
-
-
Save alexsoyes/e0c86300335a20750d9aa16b5fff118c to your computer and use it in GitHub Desktop.
Open-Closed Principle : PHP (not working)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Open/Closed principle in PHP (not working) | |
*/ | |
class User | |
{ | |
public $name; | |
public $firstname; | |
public function __construct(string $firstname, string $name) | |
{ | |
$this->firstname = $firstname; | |
$this->name = $name; | |
} | |
} | |
class Customer | |
{ | |
public $fullname; | |
public function __construct(string $fullname) | |
{ | |
$this->fullname = $fullname; | |
} | |
} | |
class AccountDisplayerService | |
{ | |
public function displayWelcomeMessage(User | Customer $entity): void | |
{ | |
if ($entity instanceof User) { | |
printf("Hello, %s %s", strtoupper($entity->name), $entity->firstname); | |
} elseif ($entity instanceof Customer) { | |
printf("Welcome again, dear %s\n", $entity->fullname); | |
} | |
} | |
} | |
$user = new User('Lucien', 'Bramard'); | |
$customer = new Customer('Mr Elliot Alderson'); | |
$accountDisplayer = new AccountDisplayerService(); | |
$accountDisplayer->displayWelcomeMessage($user); | |
$accountDisplayer->displayWelcomeMessage($customer); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment