PHPUpdatesBackend

    What's New in PHP 8.2? Features & Performance

    Jan 18, 20266 min read

    Readonly classes, DNF types, and more. Explore the modern features of PHP that you can run right here in the browser.

    PHP is Evolving#

    Gone are the days when PHP was considered a messy template language. With the release of PHP 8.0, 8.1, and 8.2, the language has adopted modern, strict, and performant paradigms found in C# and TypeScript.

    WebFiddle runs PHP 8.2 via WebAssembly, meaning you can test these features directly in your browser without setting up a local XAMPP or Docker environment.


    1. Union Types (The TypeScript Influence)#

    In the past, you relied on PHPDoc comments to tell developers a function accepts multiple types. Now, it's native.

    class NumberUtil {
        public function __construct(
            private int|float $number
        ) {}
    }
    
    function processInput(string|array $input): void {
        if (is_array($input)) {
            // ...
        }
    }
    

    This drastically reduces runtime errors and makes static analysis tools (like PHPStan) much more powerful.


    2. Named Arguments#

    Inspired by Python, you can now skip optional parameters!

    Old Way:

    // I have to pass nulls just to set the 4th argument
    setCookie('name', 'value', 0, '', '', true);
    

    New PHP 8 Way:

    setCookie(
        name: 'test',
        value: 'value',
        secure: true,
        httponly: true
    );
    

    Code becomes self-documenting. You know exactly what true stands for without looking at the function signature.


    3. Constructor Property Promotion#

    Writing repetitive code for DTOs (Data Transfer Objects) used to be painful in PHP.

    Old Way:

    class Point {
        public float $x;
        public float $y;
        public float $z;
    
        public function __construct(float $x, float $y, float $z) {
            $this->x = $x;
            $this->y = $y;
            $this->z = $z;
        }
    }
    

    New PHP 8 Way:

    class Point {
        public function __construct(
            public float $x = 0.0,
            public float $y = 0.0,
            public float $z = 0.0,
        ) {}
    }
    

    This single feature cuts class boilerplate by 60%.


    4. Match Expressions (Switch 2.0)#

    switch statements in PHP used loose comparison (==) which led to weird bugs. match uses strict comparison (===) and returns a value.

    $status = match ($statusCode) {
        200, 300 => 'success',
        400, 500 => 'error',
        default => 'unknown',
    };
    

    It’s concise, readable, and safer.


    5. Just-In-Time (JIT) Compiler#

    PHP 8 introduced JIT. Traditionally, PHP compiles your code to "Opcode" and executes it on the Zend VM. JIT takes it a step further by compiling parts of definitions Opcodes directly into Machine Code (CPU instructions).

    While this doesn't drastically speed up typical WordPress sites (which are I/O bound), it makes PHP significantly faster for CPU-intensive tasks like data processing, image manipulation, or even machine learning.

    Try It Now#

    You don't need to install anything. Open the WebFiddle PHP Sandbox and copy-paste these examples:

    Launch PHP 8.2 Sandbox

    Ready to try it yourself?

    Experience the power of WebAssembly and Node.js directly in your browser. No setup required.