PHP 7.4 is Now Available at FastComet
PHP 7.4, the last minor release before 8.0, was released for General Availability on November 28th, 2019 and as of today, it is fully available for all FastComet customers. Performance is key, and as always, PHP 7.4 does not disappoint. This post covers some of the essentials around PHP 7.4 and how FastComet customers can benefit from the overall upgrade.
Table of Contents:
Version PHP 7.4 for all FastComet Users
PHP 7.4 is available on all FastComet shared servers. Of course, make sure you check your website thoroughly after switching to the latest PHP version since some plugins may not be ready for it yet.
The boost in performance and code readability in PHP is significant with version 7.4. However, PHP 8 will be the actual milestone when it comes to performance because the proposal to include Just-in-time (JIT) has already been approved.
JIT is a compiling strategy. A program is being compiled on the fly into a form that’s usually faster, typically the host CPU’s native instruction set. To do this, the JIT compiler uses its access to dynamic runtime information. A standard compiler does not have the ability to perform this task.
The default PHP version on our servers is always selected with safety in mind. The current default version for all FastComet servers is PHP 7.2.
PHP 7.4 New Features, Updates, and Deprecations
New Features
There are lots of additions that come with version 7.4 of PHP.
Arrow Functions
Also known as Short Closures, Arrow Functions allow the usage of less verbose one-liner functions. For example, if you would previously write the following:
array_map(function (User $user) {
return $user->id;
}, $users)
Now you can write this:
array_map(fn (User $user) => $user->id, $users)
Some notes about those Arrow Functions:
- They can always access the parent scope (no need for the ‘use’ keyword);
- ‘$this’ is available exactly like normal closures;
- Arrow functions are allowed only one line. This line is also the return statement;
Read about arrow functions in-depth here.
Typed properties
Don’t we all love the new strong typing features that came with PHP 7? Each following release provided additional options and made strong typing PHP code both stronger AND easier. In the meantime, core developers have made all features optional, being aware that some users don’t want to use strong typing.
PHP 7.4 continues the same tradition and extends strong typing to class properties. With the new release, type hinting is no longer limited to method properties and return types. Additionally, developers can hint at all properties in their class.
Null Coalesce Shorter Syntax
One of the many additions that people loved in PHP 7 was the null coalesce operator. It’s used frequently in code bodies to ensure that variables have the same default. With PHP 7.4, developers are able to use a syntax that is even shorter than before. Now you can use “??=” when you want to assign a default value to a specific variable if it’s null, but leave it without changes when it has value.
Weak References
PHP 7.4 introduces the WeakReference class. In short, it offers programmers the opportunity to retain references to objects that do not prevent the objects from being destroyed.
Preloading in PHP 7.4
With PHP 7.4, support for preloading was added, a feature that has the potential to improve the performance of your code significantly.
This is how it works:
- To preload files, you have to write a custom PHP script;
- This script is being executed once on server startup;
- Preloaded files are available in memory for each request;
- Changes made to any preloaded files will not have an effect until the server gets restarted.
Preloading is built on top of opcache, but it’s not actually the same. Opcache takes your PHP source files, compiles them to “opcodes”, and then stores those files on disk.
Opcodes are a low-level representation of your code, which can be interpreted at runtime with ease. Opcache skips the step of translation between source files and what the PHP interpreter needs at runtime.
There is even more to be gained. Opcached files don’t know about other files. When you have a class A that is extending from class B, you would still need them linked together at runtime. Additionally, opcache checks to see if the source files have been modified, and then invalidate its caches based on that.
Preloading now only compiles source files to opcodes, while also linking related classes, traits, and interfaces together. Afterward, it will keep the compiled runnable code (usable by the PHP interpreter) in memory.
With PHP7.4 whenever a request arrives at the server, it can use parts of the codebase which were already loaded in memory. That’s performed without any overhead.
Updates and Deprecations
As usual, besides new features, there are also a lot of changes that come with version 7.4 of the programming language. Most of the new changes are non-breaking, but some of them could affect your codebases. Keep note that deprecation warnings should not always be defined as “breaking”, but actually as notices to developers that certain functionalities are going to be changed or removed in the future. We recommend everyone not to ignore deprecation warnings and instead fix them right on the spot. Such action would make the upgrade to PHP 8.0 much easier.
PHP 7.4 Overall Performance Benchmarks
With all PHP 7 releases, new features continue to be tacked on and the performance of the programming language continues to evolve.
According to last year’s Phoronix benchmarks, PHP 7.4 continues to improve in terms of performance.
Those improvements might not be as great as the jump from 5.6 to 7.0, but they are still present.
According to the benchmark stats, PHP 7 shows improved memory usage, as well as some other enhancements. As expected, PHP 7.4 is looking to be the fastest stable release yet, including the additions of FFI and preload functionality.
Does your Application Support it?
The great news is that most web applications, including WordPress, fully work on PHP 7.4 yet:
- WordPress 5.3;
- Joomla! 3.9.13;
- Laravel 6.7.0;
- Symfony 4.4.2 – 5.0.1;
- Grav CMS 1.6.19;
- October CMS 1.0.458;
- CodeIgniter 3.1.11 – 4.0-rc.3;
- CakePHP 3.8.7 – 4.0.0;
- PyroCMS 3.7;
- Pagekit 1.0.17;
- Bolt CMS 3.7.0;
- Craft CMS 3.4.0-beta.4;
- ExpressionEngine 5.3.0.
Sadly, some applications, such as Magento 2, no longer support PHP 5.6 but are not yet compatible with PHP 7.4.
WordPress PHP 7.4 Usage Stats
As everyone probably knows, PHP is the most used server-side programming language. According to W3Techs, as of May 28th, 2019, the usage of PHP continues to grow. With its release, WordPress 5.3 already had better compatibility with PHP 7.4.
Unfortunately, PHP 5 is still used by 53.6% of all sites with a known server-side programming language. If you add the number of users with PHP 7.0, results show that the large majority of websites run on unsupported versions of PHP.
According to WordPress’ official stats page, at the time of writing this post, an astounding 67% of the WordPress websites on the Internet run unsupported PHP versions. Websites that run on PHP 7.3 are only a little over 3%, with the majority running on PHP 5.6.
We recommend that you ask your host for a supported PHP version, where the best choice would be a release that is WordPress approved. As of the time this article is posted, the WordPress requirements are:
- PHP version 7.3 or greater;
- MySQL version 5.6 or greater OR MariaDB version 10.1 or greater;
- HTTPS support.
Preloading with Laravel
If you want to preload Laravel, your script will need to loop over all PHP files in the laravel
directory, and then include those files one by one.
During preloading, PHP resolves class dependencies and links with parent, interfaces, and traits. It removes unnecessary includes and performs some additional optimizations. opcache_reset() is not going to reload preloaded files.
Here’s how you’d link to this script in php.ini:
opcache.preload=/path/to/project/preload.php
And here’s a dummy implementation:
$files = /* An array of files you want to preload */;
foreach ($files as $file) {
opcache_compile_file($file);
}
You can’t preload an unlinked class. For files to be preloaded, their dependencies (interfaces, traits, and parent classes) have to be preloaded as well. If any problems occur with the class dependencies, you will get notified of it on server startup:
Can't preload unlinked class Illuminate\Database\Query\JoinClause: Unknown parent Illuminate\Database\Query\Builder
Luckily, there is a method to ensure that linked files are loaded. Instead of using ‘opcache_compile_file’, you can use ‘require_once’, and let the registered autoloader take care of the rest.
$files = /* All files in eg. vendor/laravel */;
foreach ($files as $file) {
require_once($file);
}
Opcache Preloading with Symfony
Symfony 4.4 can generate a preloading file for your application in the cache directory. This generated file name includes both the environment and the kernel names (e.g. var/cache/dev/App_KernelDevDebugContainer.preload.php).
You can use the generated file as the value of the opcache.preload PHP directive:
;php.ini opcache.preload=/path/to/project/var/cache/prod/App_KernelProdContainer .preload.php
The next step is allowing applications and bundles to declare which of their classes should be preloaded too.
How to Upgrade to PHP 7.4
With FastComet’s shared hosting servers, you have the convenient option to select your PHP version quickly and easily. All you have to do is:
- Go to your to Client Area (my.fastcomet.com) → cPanel → Select PHP Version:
- In the PHP Selector, click on Switch to PHP Options:
- Once in the PHP Options, select your desired PHP version and click on Set as Current:
That’s all there is to the process of upgrading to PHP 7.4.
When you are switching between different PHP versions, don’t forget to check your project compatibility before you begin with the transition.
Conclusion
This brings us to our conclusion. We only covered some of the new features, updates, and deprecations of PHP 7.4. Visit the official source if you want to see a full list of all changes.
The new PHP version looks much more powerful compared to its predecessors. Additionally, it will ease multiple development tasks for developers, because the newly updated functions are built, especially for modern web apps.
Are you willing to give it a try yet? Be sure to share your thoughts on your favorite new addition. Happy Coding, Everyone!
The latest tips and news from the industry straight to your inbox!
Join 30,000+ subscribers for exclusive access to our monthly newsletter with insider cloud, hosting and WordPress tips!
No Comments