PHP 8 — Worth The Upgrade?
What do I think of the latest version of PHP (8.1 at the time of me writing this)?
How do I love thee? Let me count the ways.
I love thee to the depth and breadth and height
My soul can reach, when feeling out of sight
For the ends of being and ideal grace.
Let me set the rollback clock to 2001. I was working at Golfsmith International, and we were tasked with rewriting the E-Commerce website in a different language. At the time, it was written using Oracle’s “htp” package. Even in 2001, we knew that this was perhaps the worst choice for web development.
The tech lead on my team, Srinivas Mulpuri, mentioned this fairly new language and platform called PHP. We ended up building a very robust e-commerce website. We even included features that seemed cutting edge in 2023 standards.
I have been developing off and on with PHP ever since. I have written code that I was proud of and code that I would want to ignore the fact that I developed it. My story is not new. It is shared by millions of PHP developers globally.
PHP has became the platform you love to hate and hate to love. But, truth be told, it is being used to power 77.4% of all websites across the globe.
Not to shabby.
What makes PHP so popular is also what has also given PHP a bad reputation for serious programming. PHP is very easy to learn and PHP applications are easy to develop. The language has always been very forgiving, with out-of-the-box configuration. Features that have become synonymous with the more enterprise languages like C# and Java, such as typed variables, object oriented architecture, and function return types, were non existent in legacy PHP applications. PHP also included features such as “magic methods”. An example of “magic methods” are implicit object getters and setters when defining classes. Once you learn about object oriented development, you will want to develop applications using object oriented architecture. With legacy PHP, you will find yourself very frustrated when trying to build a true object oriented application after you are confronted with the likes of globals and magic methods.
Now, I did not set out wanting to discuss PHP of days old. If you are reading this, you want to know, “should I upgrade”.
“Yes!”
PHP 8 and 8.1 has introduced all the constructs that are mostly familiar with enterprise software development along with an improved engine that boasts some of the best software processing times on the planet.
I found the following summary from https://kinsta.com/blog/php-vs-python/#performance-and-speed that compares the speed and performance between php 7 and python. Things only got better with PHP 8.
According to an Unbounce report published in December 2018, 79% of online shoppers facing trouble with site speed and overall performance say they won’t visit or patronize the website again.
It shows that all the apps and sites you develop must be responsive and load faster — within 2–3 seconds at most.
Although speed and performance can vary due to various elements like hardware resources, memory, storage space, code logic, hard disk access time, data path width, and more, what programming language you use is also a factor to consider.
Hence, comparing the speed and performance of Python and PHP is crucial.
The early versions of PHP were slow, including PHP 5.x that took a great deal of time to execute codes. It seems like their developer community has worked significantly on improving the programming language’s performance and speed with PHP 7.x.
It’s exceptionally faster than many programming languages, including Python. Zend Engine 3.0 was also released with PHP 7, making the programming language 2x faster than its previous version.
Comparatively, Python’s code compilation process is designed to be quicker, even without installing caching systems. When a file is created and/or modified, it converts this code into bytecode. It was way faster than what PHP used to be before PHP 7.x was introduced.
For example, if you’re developing a banking system, which can receive a huge amount of traffic daily, it needs to be exceptionally fast. Shorter delays can impact system performance greatly. In this case, using PHP 7 would be recommended over Python.
However, if you want to build a simple application where speed and time lag don’t have much impact, you can use both Python and PHP.
Conclusion: PHP wins in terms of speed and performance.
Speed and performance are not the only reasons to upgrade to PHP 8.x. For me, code quality and structure was a top reason. When you develop with an IDE, such as PHPStorm by JetBrains or Microsoft Visual Studio Code, you will see how you can improve the quality of your code through the use of the integrated linting tool.
What exactly is linting? Linting is design time validation of code standards.
Here are some features of 8 and 8.1 that the linting tool can help with as you write better quality object oriented PHP code:
- function return types
- PSR-4 code structure
- variable types
- enum types
- final class constants
- deprecation of passing null to non-null arguments
- namespace validation
- validation of array key values. causing error or warning for non-existing indices.
How about a real world example?
My SaaS software, Cognicio, was originally written using a combination Symfony components and non-framework PHP. For years, there were small bugs that sometimes occurred in only certain situations. These bugs were difficult to identify, or even recognize that they were bugs. The code was written using PHP 5.4 compatible libraries and features. The code had definite performance issues as well. After upgrading the code to 8.1, and upgrading the referenced libraries, the integrated linting tool with PHPStorm went to work! I found several sections of code where the code was written in a way that errors were absorbed.
Some people say, if you do not test it, then there are no problems to report. Therefore, you are 100% error free. This is true in code and in life.
Using the original code, I had no idea error and warning conditions were occurring. After upgrading, issues presented themselves and were fairly easy to fix. Code inefficiencies were also discovered more easily. As a result, the code ran with less bugs and performance issues were resolved.
In conclusion, to answer the question, “should you upgrade your legacy PHP applications to 8.1”? The answer is definitely, yes.
Stay tuned to other articles in my bookshelf series where I discuss other specific aspects of enterprise PHP development, such as TDD or Test Driven Design.