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

Static Arrow Function

<?php

class x {
    private $p = 5;

    function foo() {
        $b = 2;
        $f = fn($a) => $a + $b + $this->p;
        echo $f(1);         // 8 = 1 + 2 + 5 

        $t = $this;
        $f = static fn($a) => $a + $b + $t->p;
        echo $f(1);         // 8 = 1 + 2 + 5 

        $f = static fn($a) => $a + $b + $this->p;
        // Only an error if the code is executed
        //echo $f(1);        
    }
}

new x()->foo();

It is possible to add the static option to an arrow function. In that case, PHP doesn’t allow usage of the local object context, via $this.

On the other hand, it is still possible to access all the local variables of the context, and, if ever, $this was assigned to a local variable, it may be imported in the arrow function context.

Also, note that PHP accepts the presence of $this in the static arrow function, as long as… it is not executed! Since there is no way to modify the static status of the arrow function, it is a zombie function: it may be passed around, but not executed.

See Also

PHP Error Messages

PHP Features

Last updated: 10 September 2026