What’s new in PHP: the latest features and improvements

What’s new in PHP: the latest features and improvements

PHP has been the foundation of dynamic websites and web applications for years. Thanks to its flexibility, its broad community and a rich base of ready-made libraries, it has gained enormous popularity among developers all over the world. In recent years PHP has gone through a significant evolution, delivering successive versions with performance improvements, new syntax constructs and features that make it easier to write and maintain clean, secure code. Let us take a closer look at the most important changes and new features that appeared in the following releases: from PHP 8.0, through 8.1, up to the latest version 8.2.


The evolution of PHP: from version 8.0 to 8.2

PHP 8.0: a big step forward

With the release of PHP 8.0 the developer community received a number of significant improvements that changed the way applications are written in this language. The most important ones are:

  1. Just-In-Time (JIT)
    One of the most advertised features of PHP 8.0 was JIT, a mechanism of “on the fly” compilation that makes it possible to translate PHP code into processor instructions while the program is running. In practice JIT can speed up CPU-intensive calculations, although the real benefits depend on the specific use case. In typical web applications the performance gain is not always very noticeable, but in data processing tasks or mathematical operations it can turn out to be crucial.
  2. Union Types
    Another breakthrough in version 8.0 were union types, which let you indicate that a given function or property can accept more than one type. Instead of writing comments in the style of @param int|string, you can now define something like this directly in the function signature:

    function processValue(int|string $value): int|string {
        // ...
        return $value;
    }
    

    This makes the code more readable and lets static analysis tools detect errors more effectively.

  3. Named Arguments
    Named arguments make it possible to pass values to a function in any order by assigning them to specific parameter names. This makes working with functions that have many optional parameters much easier. For example:

    function createUser(string $name, int $age, bool $isAdmin = false) {
        // ...
    }
    
    // Instead of relying on the order:
    createUser('Jan', 30, true);
    
    // We can pass named arguments:
    createUser(age: 30, name: 'Jan', isAdmin: true);
    

    Such an approach improves readability and minimizes the risk of passing arguments incorrectly.

  4. Constructor Property Promotion
    This concept lets us declare class properties and initialize them in the constructor at the same time, using concise syntax. Instead of writing the property definition and the assignment in the constructor separately, we can condense it all:

    class User {
        public function __construct(
            private string $name,
            private int $age
        ) {}
    }
    

    This feature translates into less code and greater clarity.


PHP 8.1: expanding the key concepts

Version PHP 8.1 brought further improvements and added features that developers had been asking for a long time:

  1. Enumerations (Enums)
    One of the biggest novelties was the introduction of enumerated types, that is Enums. They let you define the set of possible values for a given type in one place. This is extremely useful when we want to avoid errors caused by typos in constants or by incorrect values:

    enum Status {
        case Pending;
        case InProgress;
        case Done;
    }
    
    function setStatus(Status $status) {
        // ...
    }
    
    setStatus(Status::InProgress);
    

    Thanks to this the code is better organized, and IDEs can prevent incorrect values from being entered.

  2. Fibers
    Fibers are a mechanism that supports cooperative multitasking in PHP and allows more flexible management of the execution flow. Although it is a fairly low-level and advanced tool, it provides solid foundations for building libraries and frameworks that offer asynchronous programming without callbacks or promises. In practice, fibers let you “suspend” the execution of code at one point and resume it later, which makes writing non-blocking code easier.
  3. Readonly Properties
    PHP 8.1 introduced an initial implementation of read-only properties, known as readonly. Once their value has been initialized in the constructor, it can no longer be changed. This concept is another step towards greater immutability of objects in PHP, which usually translates into fewer errors in the code and greater predictability.

PHP 8.2 – new possibilities for developers

PHP 8.2 also introduces a number of improvements aimed both at increasing performance and at making developers’ daily work easier. Thanks to them the code becomes more readable and applications can run more stably and faster. Below we take a look at the most important new features.

1. Readonly Classes

PHP 8.2 extended the concept of readonly properties to entire classes. Readonly classes allow you to define classes whose properties are all read-only. In practice this means that as soon as an object has been created (and its properties initialized in the constructor), none of the attributes can be changed any more. This makes it possible to create fully immutable objects, which significantly increases the security and predictability of the code, especially in large projects where global mutable state can lead to errors that are hard to track down.

An example of such a class may look as follows:

readonly class Config {
    public string $host;
    public int $port;

    public function __construct(string $host, int $port) {
        $this->host = $host;
        $this->port = $port;
    }
}

In the code above we will be warned already at the compilation stage (or during static analysis) if we try to change the value of either the $host or the $port property outside the constructor. This is an excellent solution for configuration objects or other entities that should never be modified while the application is running.

2. Dynamic Properties – Deprecation

In previous versions of PHP it was permitted to add properties to objects dynamically, even if they were not defined in the class. For example:

class User {
    public function __construct(public string $name) {}
}

$user = new User("Jan");
$user->surname = "Kowalski"; // Dynamically added property

As of version 8.2 dynamic properties have been marked as deprecated. What does this mean in practice? At some point this feature may be removed from the language entirely. It is a step towards a stricter, typed approach to programming in PHP. It is recommended that all properties be defined in the class explicitly or through magic mechanisms (such as __get and __set), which makes creating unplanned properties “on the fly” impossible.

3. Disjunctive Normal Form Types (DNF)

PHP 8.0 introduced Union Types, and PHP 8.1 brought Intersection Types (&). In 8.2 we went a step further and made it possible to declare types in the form of (T1 & T2) | T3, referred to as Disjunctive Normal Form. This gives even more flexibility in precisely describing the types accepted by functions or methods. For example, we can define that a parameter takes a type that is both the JsonSerializable interface and the Countable class, or something entirely different:

function processData((JsonSerializable & Countable) | string $data) {
    // ...
}

Even though such constructs may seem rare in everyday work, they are an important extension for more complex projects where precise typing is crucial.

4. Refinements and smaller changes

Apart from the big features, PHP 8.2 also introduces a number of smaller improvements, including:

  • Better memory management and optimizations in the internal engine, which translates into faster applications in specific scenarios.
  • New functions and methods in the standard libraries, among other things the capabilities related to randomness have been expanded (the Random module), which allows pseudorandom values to be generated even more effectively.
  • Continued deprecation of unsafe or rarely used functions, which aims to unify the syntax further and eliminate potentially error-prone constructs.

Why is it worth upgrading to the latest version of PHP?

  1. Performance
    New versions of PHP, starting with the 7.x line, bring noticeable performance jumps compared with earlier editions. Successive improvements, including JIT and internal optimizations, translate into faster code processing, which in turn makes it possible to handle more traffic on a site with the same hardware resources.
  2. Security
    Every new version of PHP contains security fixes. Regular updates help prevent exploits and data leaks. In addition, some of the features that can create risk (for example dynamic properties) are being phased out step by step.
  3. New features and syntax
    A more restrictive type system, support for enums, readonly classes and many other novelties make the code more readable, more secure and easier to maintain. On top of that, static code analysis tools work better with typed and modernized PHP.
  4. Community and ecosystem support
    Developers of tools and frameworks (for example Laravel, Symfony) adopt new PHP features in their projects relatively quickly. Staying with older versions, you may run into difficulties when using the newest libraries, or into gaps in documentation and community support.

Summary

PHP is developing continuously, responding to the needs of developers and to changes in the world of web technologies. Version 8.2 is another step towards a modern, fast and secure language: it offers enriched syntax, a more efficient engine and a more restrictive approach to types.

If you are planning to build new projects or develop existing applications, it is definitely worth considering an upgrade to one of the latest versions of PHP. Both 8.1 and 8.2 provide numerous conveniences (such as Enums, Readonly Properties or Readonly Classes) as well as the deprecation of older, error-prone solutions. Although this sometimes requires adjusting the code and updating the libraries in use, the benefits in terms of security and long-term support clearly outweigh the cost of migration.

Because PHP has such a strong community and well-established standards (PSR), moving to a newer version is relatively simple and painless, provided our code has been written in line with good practices from the start. For the most demanding projects there are also commercial companies offering support and consulting services, which makes a smooth transition to the latest releases even easier.

All of this means that PHP still remains one of the most popular languages for building web applications. Its versatility and considerable progress in performance and security make it an excellent choice for many development teams, both those that are just starting out with website development and those building complex applications serving millions of users.

Book a free consultation

Provide your phone number or schedule a meeting