Last active
March 22, 2021 05:51
-
-
Save alexsoyes/f74172d9bab0b1e4601fc4cee3f873ce to your computer and use it in GitHub Desktop.
Open-Closed Principle : PHP
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 | |
| */ | |
| interface NameableInterface { | |
| public function theName(): void; | |
| } | |
| class User implements NameableInterface | |
| { | |
| public $name; | |
| public $firstname; | |
| public function __construct(string $firstname, string $name) | |
| { | |
| $this->firstname = $firstname; | |
| $this->name = $name; | |
| } | |
| public function theName(): void | |
| { | |
| printf("Hello, %s %s", strtoupper($this->name), $this->firstname); | |
| } | |
| } | |
| class Customer implements NameableInterface | |
| { | |
| public $fullname; | |
| public function __construct(string $fullname) | |
| { | |
| $this->fullname = $fullname; | |
| } | |
| public function theName(): void | |
| { | |
| printf("Welcome again, dear %s\n", $this->fullname); | |
| } | |
| } | |
| class AccountDisplayerService | |
| { | |
| public function displayWelcomeMessage(NameableInterface $entity): void | |
| { | |
| $entity->theName(); | |
| } | |
| } | |
| $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