Skip to content

Instantly share code, notes, and snippets.

@alexsoyes
Last active March 22, 2021 05:52
Show Gist options
  • Save alexsoyes/e0c86300335a20750d9aa16b5fff118c to your computer and use it in GitHub Desktop.
Save alexsoyes/e0c86300335a20750d9aa16b5fff118c to your computer and use it in GitHub Desktop.
Open-Closed Principle : PHP (not working)
<?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