DEV Community

david duymelinck
david duymelinck

Posted on

1

PHP 8.5 features

I was reading a PHP 9.0 article, and I was curious about the things that are going to be in PHP 8.5. So i checked the RFC implemented section

Support Closures in constant expressions

What this RFC adds is the possibility to use a function as a default for a method/function/Attribute argument, for a global or class constant and for class properties.

At the moment following results in an error.

function test(Closure $func = static function() { echo 'default'; }) 
{
    $func();
}

test(); // Fatal error: Constant expression contains invalid operations
Enter fullscreen mode Exit fullscreen mode

In PHP 8.5 this will change to;

function test(Closure $func = static function() { echo 'default'; }) 
{
    $func();
}

test(); // default
test(function() { echo 'custom'; }); // custom
Enter fullscreen mode Exit fullscreen mode

As the first example in the RFC shows this could remove the need to use nullable type hinting.

It is also possible to have an array of closures.

function foo(
    string $input,
    array $callbacks = [
        static function ($value) {
            return \strtoupper($value);
        },
        static function ($value) {
            return \preg_replace('/[^A-Z]/', '', $value);
        },
    ]
) {
    foreach ($callbacks as $callback) {
        $input = $callback($input);
    }

    return $input;
}

foo('Hello, World!'); // HELLOWORLD
Enter fullscreen mode Exit fullscreen mode

It think it will not be long before seeing following code.

// HELLOWORLD!
foo(foo('Hello, World!'), [static function ($value) { return $value.'!'; }]);
Enter fullscreen mode Exit fullscreen mode

A possible way of abuse is by exposing private properties, constants and methods through a closure. So you have to be aware of that.

class C {
    public Closure $d = static function (C $c) {
        echo $c->secret, PHP_EOL;
    };

    public function __construct(
        private string $secret,
    ) {}
}

$c = new C('secret');
($c->d)($c); // secret
Enter fullscreen mode Exit fullscreen mode

First Class Callables in constant expressions

This RFC is related to the previous one.

One of my favorite features of PHP 8.1 first class callables will work with the closures feature.

Because the closure has to static, only static class methods can be called, which is a bit of a bummer but understandable.

// ok
#[Attr(E::myMethod(...))]
// error
#[Attr(E->myMethod(...))]
Enter fullscreen mode Exit fullscreen mode

Error Backtraces

This feature is only for fatal errors.

While we hope we never need to debug a production server, it is good to know it will be possible to get more information about an error by changing the fatal_error_backtraces ini setting to 1.

Directory class change

From PHP 8.5 it will not be possible to use new Directory().
It is a class from the PHP 4 era and it hasn't changed since then.

The way to instantiate the class is by using the dir function.

Thoughts

I think the closures features are a nice improvement.

I'm sure there will be some more features that will land when PHP 8.5 will be released.

Top comments (0)

👋 Kindness is contagious

DEV shines when you're signed in, unlocking a customized experience with features like dark mode!

Okay