refactoringguru / design-patterns-php Goto Github PK
View Code? Open in Web Editor NEWDesign Pattern Examples in PHP
Home Page: https://refactoring.guru/design-patterns/php
License: Other
Design Pattern Examples in PHP
Home Page: https://refactoring.guru/design-patterns/php
License: Other
Hi, PHP 8.2 is the current version, and PHP 8.3 will come faster than we all think, therefore I want to start a new discussion, whether we should rewrite code samples to PHP 8.2 and start using "constructor property promotion" or not. What do you think about it?
Example Adapter code with constructor property promotion:
/**
* EN: The Adapter is a class that links the Target interface and the Adaptee
* class. In this case, it allows the application to send notifications using
* Slack API.
*
* RU: Адаптер – класс, который связывает Целевой интерфейс и Адаптируемый
* класс. Это позволяет приложению использовать Slack API для отправки
* уведомлений.
*/
class SlackNotification implements Notification
{
public function __construct(private readonly SlackApi $slack, private readonly string $chatId)
{
}
/**
* EN: An Adapter is not only capable of adapting interfaces, but it can
* also convert incoming data to the format required by the Adaptee.
*
* RU: Адаптер способен адаптировать интерфейсы и преобразовывать входные
* данные в формат, необходимый Адаптируемому классу.
*/
public function send(string $title, string $message): void
{
$slackMessage = "#" . $title . "# " . strip_tags($message);
$this->slack->logIn();
$this->slack->sendMessage($this->chatId, $slackMessage);
}
}
Hello, I have noticed one tricky thing in a conceptual example.
Here is an interface:
interface Memento
{
public function getName(): string;
public function getDate(): string;
}
and here is restore() method from originator:
public function restore(Memento $memento): void
{
$this->state = $memento->getState();
echo "Originator: My state has changed to: {$this->state}\n";
}
I don't really understand how we can call getState method if interface doesn't declare it ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.