Reader small image

You're reading from  Clean Code in PHP

Product typeBook
Published inOct 2022
PublisherPackt
ISBN-139781804613870
Edition1st Edition
Tools
Right arrow
Authors (2):
Carsten Windler
Carsten Windler
author image
Carsten Windler

Carsten Windler is a Lead PHP engineer at Plan A.
Read more about Carsten Windler

Alexandre Daubois
Alexandre Daubois
author image
Alexandre Daubois

Alexandre Daubois is a Symfony Developer at SensioLabs, the company that created Symfony.
Read more about Alexandre Daubois

View More author details
Right arrow

Optimizing Your Time and Separating Responsibilities

It’s time for a little practice after all that theory! We’ve already seen a lot together: advanced principles on clean code, how to choose the right external libraries for your applications, and how to take advantage of the latest patches of these libraries while not risking making your project explode in mid-air. But we should not forget that there is the word “code” in “clean code” (obviously). In this chapter, we will therefore concentrate a little more on the source code of your application and see the following points:

  • Naming conventions and organization of files and folders
  • Why is it important to separate responsibilities to respect the “S” of the SOLID principles? What does it bring to you?
  • We’ll discover an elegant way to manage responsibility separation with an event system
  • And we’ll finish with some polymorphism—namely, abstract...

Naming and organizational conventions

We must add a disclaimer before anything else. The naming conventions and organizational ideas given in this chapter are not an absolute truth. As we have seen before, the most important thing is to respect the conventions already in place in your project and to be consistent with your team. If you feel it is necessary, it is possible to adapt these rules to your needs. Again, the important thing is to use common sense and logic and to be as clear as possible.

Let’s first talk about the naming of source files. Obviously, the naming conventions differ from one technology to another (depending on whether you use a certain framework or another, the good practices may change, for example). Nevertheless, we can note some conventions that can be found almost everywhere.

Class files and interface files

PHP: Hypertext Preprocessor (PHP) source files defining a class, an abstract class, or an interface should have the same name as the class...

Separating responsibilities

Let’s see what the separation of responsibilities in the code consists of, to make it cleaner, understandable, maintainable, and extensible. This is the first point of the SOLID principles. In the second chapter, this is how we defined the principle of single responsibility: “It means that a class in your code must respond to only one task.

As a reminder, SOLID is a set of known clean-code rules that, when applied together, will make your code much clearer and more accurate. Rather than trying to follow the five principles described by each of the SOLID caps to the letter, it is more important to have a global idea of all this in mind when you code.

The first step to respect this is, in fact... naming, as we just saw! Indeed, by naming a class properly, clearly, and, above all, precisely, you are already making sure that it doesn’t become a mess where you put a little bit of everything that you can think of. And it is for...

Demystifying polymorphism – interfaces and abstract classes

As far as the separation of responsibilities is concerned, event dispatching is a concept that is already advanced. You can consider that your level in the world of clean code has increased considerably if you know this mechanism, understand it, and have the chance to use it. All this obviously requires a bit of setup. Either you implement this system by yourself or you use an external library. In the second case, there is obviously a whole learning phase to be included. Either way, this is obviously not the only way to improve the separation of your responsibilities. There is a way that is native to PHP to improve this separation, sometimes not used enough, sometimes misunderstood, and often underestimated. We are talking here about polymorphism, or vulgarly: abstract classes and interfaces.

First, why the word polymorphism? Poly comes from the Greek meaning many, and morphism means form/shape. Abstract classes...

Summary

We have just covered the most advanced part of the theoretical section of this book. We are now armed with the knowledge to cut our code cleanly while keeping it maintainable and extensible for future developers. It will also be ready for the future by being strongly open to extension and closed to modification (as described in one of the SOLID principles).

We have reviewed many of the cases that you may encounter regarding the naming of files, classes, and methods when developing a PHP application. In addition, we have seen that folders must have specific names and can be used to divide your application into different domains.

The separation of responsibilities was also a big topic. It is particularly important to understand why this separation is useful, even vital, in a project. It is the real key to a well-architected project that is easy to navigate. Event dispatching is an excellent way to achieve this, as we have seen. Event dispatching is one of the cornerstones...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Clean Code in PHP
Published in: Oct 2022Publisher: PacktISBN-13: 9781804613870
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Authors (2)

author image
Carsten Windler

Carsten Windler is a Lead PHP engineer at Plan A.
Read more about Carsten Windler

author image
Alexandre Daubois

Alexandre Daubois is a Symfony Developer at SensioLabs, the company that created Symfony.
Read more about Alexandre Daubois