The newest version of PHP is under development, and reaching the end of its ‘beta’ phase - so here’s a quick round-up of what’s coming in PHP 8.1 - due for release in November 2021.
Every release of PHP in more recent years adds some great new features, and PHP 8.1 is no different. Whether you're a developer who wants to see the programming language mature to ensure your code is secure and efficient, or you're a PHP web hosting user whose need is for speed!
Take a look at our quick guide to some of the more exciting features:
What’s new for hosters?
OPcache is PHP’s main performance-enhancing cache, which has been a standard feature since PHP 5.5. Its purpose is to precompile PHP scripts into special bytecode, and it stores these in shared memory. This removes the need for PHP to load and parse scripts on each request - meaning frequently requested pages load a lot faster! PHP 8.1 pushes OPcache development further allowing links between classes to be cached too - delivering a performance boost of up to 8%. Pretty neat, eh?
What’s new for developers?
If you’re a PHP language aficionado you’ll love some of the new features that PHP 8.1 brings.
Enums are here!
“Enums” or “Enumerated Types” as they’re properly known make their debut in the latest PHP release. No longer will developers have to assign numbers against status and ensure they don’t clash. If you’re used to other programming languages you’ll take to these like a duck to water.
If you’ve ever wanted to declare a class property as read-only, and not rely on linters or static analysis to check that someone isn't trying to change the value - you’re in luck! The “readonly” modifier can be added to a property, and if someone tries to change the value, an error will occur.
What’s smaller than a thread? A fiber!
While you’ll have to excuse the Americanised spelling, fibers provide a way to parallelise code, and hence make execution faster. Also known as ‘coroutines’ or ‘green-threads’, fibers can be started, stopped, or just suspended from anywhere within your code. Unlike threads they will also be destroyed cleanly when references to their object-oriented form ends.
You may never need to use fiber yourself - but we’re sure some of the larger PHP frameworks sure will.
Previously called ‘noreturn’, the newly renamed ‘never’ type indicates that a function will stop the program flow. This explicitly allows a function to indicate that it may ‘exit()’ or throw an exception. This is useful for static analysis tools (like Psalm and PHPStan) and some IDEs (like PhpStorm) for identifying errors that can creep into code.
For heavy array users, there are a few magical (see what we did there?) functions being added to help with array manipulation. Firstly ‘array_is_list()’ allows you to tell the difference between an associative array, and a plain old list array. PHP 7.4 added the spread operator (...) for unpacking, however that could only be used on arrays with numeric keys. PHP 8.1 allows you to unpack arrays with keys too, using the same operator.
Within PHP constants in classes can be overridden through inheritance. In PHP 8.1, you can use the modifier ‘final’, to indicate that a constant really should be … constant!
At Fasthosts we optimise our platforms, so whether you’ve got a WordPress site or a Web Hosting site with us - you’ll get the benefits that PHP 8.1 delivers. There’s a whole lot more coming in the next release that we haven’t mentioned here, you can read in full the changes going into PHP 8.1.