Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Clean Code in PHP

You're reading from  Clean Code in PHP

Product type Book
Published in Oct 2022
Publisher Packt
ISBN-13 9781804613870
Pages 264 pages
Edition 1st Edition
Languages
Authors (2):
Carsten Windler Carsten Windler
Profile icon Carsten Windler
Alexandre Daubois Alexandre Daubois
Profile icon Alexandre Daubois
View More author details

Table of Contents (18) Chapters

Preface 1. Part 1 – Introducing Clean Code
2. Chapter 1: What Is Clean Code and Why Should You Care? 3. Chapter 2: Who Gets to Decide What “Good Practices” Are? 4. Chapter 3: Code, Don’t Do Stunts 5. Chapter 4: It is about More Than Just Code 6. Chapter 5: Optimizing Your Time and Separating Responsibilities 7. Chapter 6: PHP is Evolving – Deprecations and Revolutions 8. Part 2 – Maintaining Code Quality
9. Chapter 7: Code Quality Tools 10. Chapter 8: Code Quality Metrics 11. Chapter 9: Organizing PHP Quality Tools 12. Chapter 10: Automated Testing 13. Chapter 11: Continuous Integration 14. Chapter 12: Working in a Team 15. Chapter 13: Creating Effective Documentation 16. Index 17. Other Books You May Enjoy

It is about More Than Just Code

Wouldn’t describing PHP: Hypertext Preprocessor (PHP) as a programming language be a bit reductive when you think about it? We must face the facts: PHP is not a simple programming language. It’s a complete ecosystem, with a gigantic community, thousands of contributors, and new features being proposed and released regularly. But not only that: millions of libraries and application programming interfaces (API) are written and launched thanks to PHP. Even many command-line tools are entirely developed thanks to the PHP language. PHP is a whole world on its own. Let’s start by looking at the reasons why PHP is not just a language for writing a website.

These are the topics we will cover in this chapter:

  • PHP as an ecosystem
  • Choosing the right libraries
  • A word about semantic versioning
  • Stability versus trends

PHP as an ecosystem

This can be seen in several things that we can list together, as follows:

  • PHP is, still in the early 2020s, the most used server-side language for web application development. When you know the predominant (not to say overwhelming) place of web applications in our everyday use, this is a genuinely nice award!
  • The language continues to evolve very strongly, especially in recent years. It went through a slump during the development of PHP 6 (which was never released) before experiencing a real explosion of its popularity starting with version 7. Version 7 defined the foundation of the future of PHP with highly demanded features such as strong typing, as well as incredible performance and speed improvement. Benchmarks comparing PHP 5 and 7 were just crazy when they first came out. Developments continue strongly, with new features being proposed very regularly.
  • PHP has an exceptional dependency manager named Composer. Simple, open source, and devilishly...

Choosing the right libraries

Choosing the right external library to install can be a real challenge. It’s a challenge we’ve all faced or will all face one day. The reason is simple: there is no point in reinventing the wheel. The reason we want to install an external library is usually the same. We have a specific problem that we want to solve as cleanly as possible. Here, two situations arise:

  • We know how to solve the problem, but we don’t want to have to rewrite everything when tools already exist to solve our problem simply
  • We have no idea how to solve the problem because we lack theoretical or practical knowledge

It is then interesting to call upon an external library whose role is to bring us a very specific solution to our case. The advantages are multiple, as outlined here:

  • The person(s) who develop(s) the external library may have thought for several days or weeks about the best way to provide a solution. It may even be their...

A word about semantic versioning

Speaking of updates, let’s talk about versioning and—especially—semantic versioning. If the external library you want to use follows the rules of semantic versioning, this could have an incredibly positive and reassuring impact on your developments and updates. Let’s take a look at what this means exactly.

What is semantic versioning?

Versioning is simply putting a number on a version of the source code. We are all familiar with versions such as 1.0, 1.5.0, 2.0.0, and so on. The semantic versioning adds a semantic—that is to say, precise meaning to each of these numbers. Let’s take version 2.3.15 as an example. Here is how semantic versioning breaks down this version number:

  • The “2” indicates a major version. A major version can introduce new features, bug fixes, and—most importantly—changes that break backward compatibility. This last point is the most important. Indeed...

Stability versus trends

Let’s finish this chapter with a few words about the most recent versions, but also about trendy external technologies and libraries.

First, let’s talk about the latest versions of external libraries. Of course, we might be tempted to use the latest ones, the ones that were just released a few hours ago. It is worth remembering that bugs may appear, and a new patch version may be released in the near future if this is the case. Or not. And in this case, the bug could persist for a while. So, it’s particularly important to write tests. Imagine the comfort: you update all your dependencies, you run your test suite, and if all the lights are green (and your application is properly tested), you can be fairly sure that everything is fine.

That said, if any tests turn red because you’ve updated an external library, you’ll have to investigate to find out where this is coming from. In any case, you shouldn’t think that...

Summary

Limiting PHP to the programming language is reductive. We have just seen it—it is a real ecosystem with a rich and active community, and extremely far from burying its favorite language. The developments around PHP are countless, and the language itself has evolved in the most beautiful way in recent years. The contributions of functionalities gave a real second wind to this one, allowing it to claim—still today—first place among the most used programming languages on the server side for a web application.

All this would be nothing without the explosion in the number of external libraries available for the language. You have a problem; there is a solution. We are fortunate that most external libraries are open source. Thousands of developers make available, voluntarily and free of charge, the fruit of hours, weeks, or years of work.

Making a choice from among these libraries can be difficult and challenging. It is important, even mandatory, to do...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Clean Code in PHP
Published in: Oct 2022 Publisher: Packt ISBN-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.
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 $15.99/month. Cancel anytime}