Cannot use dynamic function name in constant expression

Description

This error appears when the name of the function cannot be entirely resolve at compile time. Here, the array cannot be turned into a function or static method name. This also happens for variations on the name, such as using a boolean, an array, or any expression that cannot be resolved at compilation time.

Example

<?php

const Closure1 = ([])(...);
const Closure2 = (-1)(...);
const Closure3 = (true)(...);

var_dump(Closure);

?>

Solutions

  • Use a string for the name of the function or static method.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.