If you have a web product built using PHP, then you’ve already appreciated the benefits of this programming language. However, you need to use the latest technology version to get the most out of it. At the same time, PHP 8 became available in 2020, but statistics tell us that over 80% of PHP sites are still running on PHP 7.4 and even older versions. Perhaps yours is among them.
We understand your risks and concerns. It is always hard to change something on the fly, that is, on an operating application, so that users do not feel inconvenienced. The bigger the project, the higher the risks of malfunction during migration. In the meantime, moving to PHP 8 is inevitable to keep your product up-to-date.
You don’t have to figure out the intricacies of how to migrate to PHP 8 on your own because our PHP migration guide will save you time and help avoid losses. Together, we will go through the PHP 7 to 8 migration step-by-step without putting your system and data at risk.
What’s New in PHP 8: 3 Main Features and Improvements
PHP 8 continues the course started with changes in PHP 7 to improve performance and perfect the syntax.
1. Just-in-time (JIT) compilation
The main novelty and even highlight of PHP 8 is the introduced Just-in-time (JIT) compilation. JIT compiles code during the run time of a program. Parts of PHP code can be compiled into machine code and translated directly to the CPU without interpretation on the PHP side. In this way, the software can run faster. This is what the interpretation looks like with the JIT in place:
What did the introduction of the JIT compilation bring? Not always, but quite often, JIT increases performance. This has to be measured on a case-by-case basis, and the settings here are also significant.
The point is that JIT is aimed at optimizing operations with the CPU. Accordingly, for those applications and websites where I/O (Input/Output) operations are most important, the impact of JIT compilation on performance will be less significant.
The JIT compilation expands the range of possibilities for application development in PHP. The main thing is an acceleration of computational operations to a level comparable to the speed of compiled languages.
2. Zend Engine
The increase in performance and extensibility is largely also due to the improvement of PHP’s compiler and runtime environment, which is Zend Engine.
Changes affected the management of memory, resources, and other standard services. Traditionally for PHP, the release of a new version of the language is accompanied by an upgrade of the runtime engine. At one time, Zend Engine 3 was built for PHP 7. When you migrate to PHP 8, you switch to using Zend Engine 4. The latest version of Zend Engine was developed specifically for PHP 8 and designed to further improve performance. So now the РНР runs with a new heart!
In any case, the comparison of many operating parameters of web systems is in favor of the eighth version of PHP. As for web applications, even with a comparable script execution time, PHP 8 will use less memory. As for websites, on PHP 8.0 the page will most likely load faster than on PHP 7.4.
3. Attributes
Let’s look at a local but helpful new detail that will come in handy after your migration in PHP. Attributes for adding metadata to classes and methods have appeared. Thus, in PHP, the structured way of specifying metadata has replaced the PHPDoc comments used in previous versions. If the analogy with annotations in other programming languages comes to mind, then yes, it is relevant.
We do not see the point in describing all, less extensive, changes, in particular, to the syntax since not all readers personally write code. Let’s focus on how to migrate from PHP 7.4 to 8.
When deciding, for example, to migrate PHP 7 to 8, companies take into account the deadlines for supporting previous versions. At the moment, the terms of both active support and secure support for PHP 7.4 have already expired, and even more so for older versions. We have already detailed in our PHP Application Modernization article the topic of why it is in the interests of businesses to operate their websites or web apps in supported language versions.
Top 3 PHP Migration Tools
Now let’s talk about the top 3 tools that will be useful for migration from PHP 7 to PHP 8, including quality assurance and verification.
1. Rector
Rector occupies a prominent place in the arsenal of PHP developers, not only facilitating upgrading and refactoring but also automating them. This tool can be run in almost any PHP project. Its use brings significant time savings. Updating PHP and its frameworks, as well as improving code quality, are among Rector’s supported scenarios.
2. PHPStan
PHPStan scans the code and helps identify issues and bugs, even potential ones. This tool is legacy-friendly, which makes it easy to use for code refactoring and upgrading. In some scenarios, it is quite possible to consider using PHPStan as an alternative to testing.
3. PHP_CodeSniffer
PHP_CodeSniffer (PHPCS) analyzes PHP files, checks the code for compliance with coding standards, and shows violations. In doing so, you can pre-establish a set of standards that your development team will follow. Among the PHP Standard Recommendation (PSR), PSR-12 Extended Coding Style Guide should be mentioned foremost. It is helpful that PHPCS not only detects violations of standards but also helps to automatically correct some shortcomings of code style.
Saving working hours and, therefore, the cost of code development and maintenance is an opposite consequence of using all the above PHP migration tools.
8 Steps to Migrate from PHP 7 to PHP 8
Now let’s go through the PHP 7 to 8 migration path without leaving any gaps. Of course, we will need the PHP migration tools we mentioned.
Step 0. Preparing to migrate to PHP 8
First of all, you need to containerize the application. You can use Docker. Docker will help you switch between different versions of PHP on the same machine.
Start using Docker if you haven’t already.
Step 1. Versions of your framework and libraries
Make sure you are going to use a version of the framework which works with PHP 8. x by checking your framework`s documentation. If you have an outdated version, upgrade it first, if possible. There are different guides for different frameworks.
The same situation applies to libraries used in the project. Do they support PHP 8.x? Upgrade them if possible. Overall, 7.4 is the latest release of 7.x and is as close as possible to 8.x. Therefore, it turns out that, after switching to 7.4, it is easier to switch to 8.x. But everything depends on the specific case, and you can immediately go to the PHP 8 version.
Step 2. Upgrade the version of PHP
The simplest way to switch a version of PHP is Docker. So I switch the PHP version in my docker-compose.yml file from 7.4.33 to 8.2.6
After that, run your project using Docker Compose with the following command:
You can open your project, but there is a high chance that you will get an error message.
At this point, your project is running on PHP 8.2.6, but the source code is not ready for PHP 8.2
Step 3. Update libraries with composer
Open composer.json and update the “require.php” variable.
Switch it to PHP 8:
After that, run the Composer update command:
If you had issues with the framework upgrade on Step 1 due to your PHP version, now is the time to do so. Follow the upgrade guides for your framework and libraries.
Step 4. Fix code issues
Most often, changing versions causes bugs related to backward compatibility. Something new may not work as before. For example, the function name may change. Rector is the most effective way to upgrade your source code so it is ready to use with PHP 8.x. This tool helps you to update your code for newer versions of PHP. Follow the Rector documentation with installation instructions and install it.
In our Docker case, we used the following commands:
The last command created rector.php in the root folder of my Symfony project. Change rector.php to the rules that you need. The full list of rules is described here.
The most important rule is SetList::PHP_82 because we are going to use PHP 8.2
Also, pay attention to the paths method. This is an array of folders that will be scanned by the Rector tool. For example, the final rector.php might look like this:
Try Rector in preview changes mode:
Make sure it works.
Apply the changes.
That’s it. Depending on the version of your framework, you may need to perform some extra steps, like clearing the cache. Sometimes the easiest way to get the effect is to restart Docker.
Step 5. Code style (optional)
If you are required to maintain code quality according to international standards, for PHP, it is PSR-12, follow this step. This will keep the code in a quality state that is easy to read and potentially more understandable to new developers who haven’t worked with the project before. Install PHP_CodeSniffer.
You can do this with the following command:
where src – path to the source code of the project to be analyzed.
Analyze the list of issues. You can fix most issues automatically with the following command:
Step 6. Verify results with automation tools
You can use PHPStan to verify that you do not have any issues. Rector already includes PHPStan. So there you have it.
Analyze the changes with the following command:
where src – path to the source code of the project to be analyzed.
It should return a “No errors” message. If you have any errors – fix them.
Do you have unit tests with PHPUnit? It’s a good time to run them.
Do you have an API test? Do them.
Run all available tests. You have to make sure that everything works fine and functionality of your project works as it should.
Step 7. Verify results manually
Note that automated testing may not always detect all system vulnerabilities. So use manual testing as well. Check everything manually. Fix issues if you see something is wrong. Ask the QA engineers for assistance.
Everything is done. You have completed PHP 7 to PHP 8 migration. You now have an upgraded version of PHP.
How MobiDev Can Help You Migrate to PHP 8
Of course, each company may have many versatile tasks that are more urgent than updating outdated systems. Especially since these web applications and websites still seem to be working. Limitations and vulnerabilities of legacy apps can still be tolerated, although their presence might be gradually growing and becoming annoying.
Everyone decides individually when exactly to carry out PHP 7 to 8 migration. We advise you to do it before suffering security gaps, crashes, increased downtime, UX degradation, etc. And before adding new advanced features will have to be delayed indefinitely. In a nutshell, before users start getting frustrated with your website or web app.
Commonly, when your team is about to do something for the first time, there are additional questions. Our software engineers have accumulated considerable experience in migration in PHP. We will willingly guide you through this path without a hitch.
You can rely on a well-developed infrastructure to use the full range of PHP capabilities. At MobiDev, use appropriate libraries and tools that greatly enrich the web development in this language. Without Symfony and Laravel, for example, PHP would hardly be where it is at the top of web development today. These popular frameworks also adapt to migration from PHP 7 to PHP 8.
The MobiDev PHP team regularly tests the current versions of Symfony with PHP 8. Specifically, we ran Symfony 5.4.23 with PHP 8.2.4 and were pleased with what we saw. Note that we used not only the out-of-the-box version but also libraries such as Doctrine, StofDoctrineExtensionsBundle, RedisBundle, EasyAdmin, etc., which are in high demand. Thanks to our tests, we have reason to conclude that the PHP 8 migration went well.
Thus, we can safely say that PHP 8 has outstanding potential for web application development! Сombining with popular frameworks and libraries significantly contributes to realizing this potential.
If you have hardships with technology changes or lack the relevant expertise, get in touch with us. Rely on our app modernization services to update your PHP product.