Cannot create Closure for new expression

Description

When using the ... ellpsis operator as an argument, it creates a closure, based on the current function or method. While the syntax can be linted by PHP, it is not possible to use it with a new operator.

Example

<?php

class X {
    function __construct() {}
}

$closure = new X(...);

?>

Solutions

  • Make a closure that takes the arguments, and relay them to the new operator.

  • Create a named constructor, and make a closure from this.

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>`_.