Compatible Signature

By Christian Lück

<?php

class CallbackWithIntersectionTypehintClass {

    #[PHP8] public function __invoke(Exception&Countable $e) : void /*
    // public function __invoke(bool $feelingDirty = true): void  // */
    { echo __METHOD__; }
}

(new CallbackWithIntersectionTypehintClass)();

This piece of code shows a signature that changes with the version of PHP.

Indeed, in PHP 7.x, the comment starts with # and goes to the end of the line. This also covers the /* so the multiline comment doesn’t start, and the __invoke signature is the second one.

In PHP 8.0, the attributes #[PHP8] ends with its closing square bracket, and the signature of __invoke starts. By the end of the line, a multi-line comments starts, and covers the whole second line.

In the end, this code stays unchanged between PHP versions, although it toggles different signatures.

See Also

PHP Features

Last updated: 14 July 2026