Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Stateless Static Closures Are Cached And Reused

A static closure that captures nothing, no use() variables, no $this, is a stateless closure. Until PHP 8.6, each evaluation of such a closure literal created a brand new Closure object, so two closures built from the exact same code were never identical. In PHP 8.6, the engine creates the object once and reuses it on every subsequent evaluation, so two stateless static closures built from the same code are now the same object.

PHP code

<?php

function make() {
    return static function () {
        return 1;
    };
}

var_dump(make() === make());

?>

Before

bool(false)

After

bool(true)

PHP version change

This behavior changed in 8.6.

See Also

Extension

© 2023-2026, Damien Seguy - Exakat