While there is a difference between knowing something and knowing the name of something; the former method gives importance to knowing the name of something and the latter one ensures that a person actually knows and understands something.
This Richard Feynman technique states that the ultimate test of your knowledge is when you can convey it to another person.
Considering this, there are many concepts of Drupal 8 based on object-oriented PHP, which we might not have nailed it earlier, so let’s get a good refresher on the same to see its implementation as well as the use cases. This blog will focus on the following elements-
A quick note before we start-
Pragmatic programming is all about teaching yourself, implementing what you've learned, and sharing the knowledge. Sharing allows you to get more feedback and useful insights. So, learn more, and share more!
Design patterns describe the communicating objects and classes that are customized to solve a problem in a particular context.
In simple words, they are patterns or templates that can be implemented to solve a problem in different particular solutions.
Why are they required? (Design Patterns’ Superpowers)
The categorization of GoF (Gang of Four Design Patterns)
They are called GoF Design Patterns because the patterns were originally written and designed by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides,
Let's start with :
It is the creational design pattern that doesn’t allow the system to create more than one instance of the class.
Benefits -
Let's take a look at the example which explains it's working. This example considers that you are familiar with creating a custom module and creating routing and controllers in Drupal 8-
The factory method is used for creating objects instead of making direct constructor calls. The factory pattern just separates the process of "object creation" from the "business logic" by creating a "factory class" whose sole responsibility is to create "objects".
Extended classes can override this method to change the class of objects that will be created.
This is a pretty simple example of understanding factory patterns, where multiple objects creation is involved based upon some business logic.
Let's look at the example to understand this in a better way.
This method contains the business logic for user role creation and returning the user-created with its role and user name.
This is how the separation of logic and object creation works in factory patterns.
Note: The ideal factory pattern will include the dynamic object creation logic. To keep it simple here, I have hardcoded the user role in if condition to create an object of a specific class.
3. Mediator design patternThe mediator is a behavioral design pattern that is used to reduce the dependency between the components and act as a mediator between them by handling the communication between them.
One of the best examples in Drupal 8 for the Mediator design pattern is events. It allows several components to interact with each other using a mediator object.
As the class diagram shows, the Mediator has the responsibility to carry out the communication between the classes and the objects.
Using the example mentioned in https://www.drupal.org/docs/8/creating-custom-modules/subscribe-to-and-dispatch-events, I created a simple implementation in Drupal which subscribes to a specific event and fires an action based upon the event triggered.
Based upon the event-triggered ConfigEvents::Save, I am displaying the message with the name of the configuration saved and action performed on it as follows-
4. Introspection and Reflection
Introspection in a programming language can be defined as a process through which developers can manipulate objects and classes.
It is useful when the execution of classes or methods is unknown at design time.
In PHP, it provides the ability to examine classes, interfaces, properties, and methods. PHP offers a large number of functions that you can use to accomplish the task.
PHP Introspection Functions:
PHP supports reflection through its Reflection API class. It provides a large number of classes and methods that can be used to achieve reflection tasks.
You can learn more on the same from here-
5. Services and Dependency injection
Drupal 8 introduces the concept of services and dependency injection that can be used to write reusable functions and keep them at a single place in a way that these are pluggable and replaceable as well.
Services and dependency injections are the best way to make reusable and pluggable components
They are design patterns in itself which can be categorized under the Creational Pattern, and where the object creation has a specific pattern based upon some business logic.
They are ready to use code blocks that pull in the required code functionality and can be used to avoid writing the same piece of code multiple times.
You can learn more about services and dependency injections here.
This brings us to the end of our discussion about "Revisiting the PHP programming concepts in Drupal 8". However, there are so many other important PHP concepts as well which should be revisited time and again to ensure easy implementation, whenever required.
Never stop learning!