Why PHP in the first place?
- it’s a widely-used, open-source scripting language
- it’s executed on the server
- it’s free to download and use
- It’s used to build/manage dynamic content, databases, session tracking, e-commerce sites
- Approx 78% of all websites run completely/partially with PHP as a server-side language
But one thing that stands apart from other server side programming languages is that PHP is one of the programming languages that is the easiest to learn and adapt.
The current version of PHP i.e. 7 feels familiar to its predecessors PHP 5.4 but has been optimized and tuned for performance. Version 8 on the other hand further improves on this and introduces other new features.
PHP 8 is doing its alpha testing circles around the web for quite some time now and is expected to be released in December 2020, and it’s bringing a bunch of new features and language improvements.
What you should expect from this release?
As PHP 8 is a major release, one should expect your old code would no longer be compatible. But most of these complications were already addressed in versions 7.x releases. So if your current code is up-to-date then you won’t have a problem in migration.
Let’s see some of the upgrades:
- Backward Incompatible Changes
- Changes in SAPI modules
- Deprecated Functionality
- New Functions
- New Classes and Interfaces
- Removed Extensions and SAPIs
- New Global Constants
- Changes to INI File Handling
- Windows Support
- Performance Improvements
A complete list of changes can be found in the upgrade notes on GitHub
Top 8 Features in the New Version of PHP 8:
- Preloading
PHP can be configured to preload scripts into the opcache when the engine starts. Any symbols (functions, classes, etc.) in those files will then become globally available for all requests without needing to be explicitly included. Note that the optimal tradeoff between performance and memory may vary with the application. preload.php is an arbitrary file that will run once at server startup (PHP-FPM, mod_php, etc.) and load code into persistent memory i.e. any file referenced by include, include_once, require, require_once, or opcache_compile_file()
opcache.preload=preload.php
- OPcache Extension
OPcache has been recently improved with the implementation of preloading. Being an interpreted language, PHP needs an interpreter which leads to increased use of computer resources but also takes a lot of time. CPU resources are often the most strained. Preloading provides a way to store a specified set of scripts into OPcache memory “before any application code is run” - JSON extension activated
The PHP native JSON extension has been bundled and compiled with PHP by default since 5.2 but it needs to be manually activated from the INI. However, from PHP 8 this extension will be activated at all times. and is assumed that this simplifies working with PHP developers.
You would also notice some noticeable changes related to more strict compliance with the JSON RFC. - Constructor property promotion
Constructor Property Promotion is a shorthand syntax to declare and assign class properties from the constructor. It allows you to combine class fields, constructor definition and variable assignments all into one syntax.
Promoted parameters may occur inside trait constructors. This avoids having to type the class property name from 4 times to just once, and property type from twice to just once.
Promoted properties have to be prefixed by one of the visibility keywords i.e. public, private, protectedclass Animal{ public function __construct(private string $type) { echo $this->type; } } new User('Kittie'); # echos "Kittie"
- Match expression
A powerful feature that will often be the better choice to using switch. Although the name suggests a certain link with regular expressions, it is rather close to the switch command.
Thanks to this mechanism, developers will be able to return the value based on the input parameter, without using additional keywords, i.e. break, return.
What’s more, Match uses strong typing to compare values (similar to using the === comparison expression), and if none of the cases are met, the UnhandeledMatchError exception is thrown by default.
Here’s its match equivalent:switch ($statusCode) { case 200: $message = "DynamicContent"; break; case 301: case 302: case 303: $message = "RedirectPage"; break; case 404: $message = "Not found"; break; default: $message = "Unknown status code"; break; }
match ($status_code) { 200 => "DynamicContent", 301, 302, 303 => "RedirectPage", 404 => "PageNotFound", default => "unknown status code" };<br />
- Weak Maps
Weak maps allow creating a map from objects to arbitrary values without preventing the objects that are used as keys from being garbage collected.
WeakRefs were already added in PHP 7.4. With PHP 8, however, WeakMaps have been included which are the extension of this function. This will allow a better way of managing and processing its objects.
All this will increase productivity by reducing the waiting time for a request for example. WeakMaps and WeakRefs can be used to delete objects when only the cache references the entity classes of the objects.
$map = new WeakMap; $obj = new stdClass; $map[$obj] = 42; var_dump($map); /* Output: object(WeakMap)#1 (1) { [0]=> array(2) { ["key"]=> object(stdClass)#2 (0) { } ["value"]=> int(42) } } */ // The object is destroyed here, and the key is automatically removed from the weak map. unset($obj); var_dump($map); /* Output: object(WeakMap)#1 (0) { } */
- JIT (Just In-Time Compiler)
Among the major new features of PHP 8, is the JIT compiler, which promises to improve performance significantly. As we know PHP is not compiled, but interpreted line by line, the JIT translates the extreme parts of the intermediate code into machine code.
However, these improvements would not be noticeable in the context of web requests, but rather in heavy data-driven usage. - Union Types
Union Types are well known concept in programming languages like C/C++, TypeScript etc. Union types can be useful in a variety of cases and are a collection of two or more types which indicate both for input and output data. This will also allow several types to be defined for the arguments received by a function, as well as for the value it returns.
Until now, the syntax of the language did not allow for the use of the Union but this has been solved by defining types in annotations.
class Animal { private int|string $type; public function setType(int|string $type): void { $this->type = $type; } public function getType(): int|string { return $this->type; } }
Conclusion
PHP 8 may not be the monster of an update like PHP 7.0-7.4 was, but PHP 8 is a big step towards tidying up the language and a release that brings many long-awaited features.
Though in this article I have tried to explain some of the important features and upgrades that I feel is important to understand, there are many more that might feel right and that fits your context of implementation. (Read more on https://php.watch/versions/8.0)
However, JIT compiler opens up new horizons for the language, I have kept it brief and I would love to talk about JIT compiler in a separate article.
As a developer, it’s a challenge to use all the best functions and optimization techniques in our development cycle, but I would love to hear about What part of the PHP 8 update will you be using the most?