Skip to content

[Console] Cannot call setCode with static closures #20845

Closed
@araines

Description

@araines

Following this commit:
symfony/console@9f7a193#diff-25a75e093c4a0d5564a36cebbb1384b3R290

I believe a BC break was introduced in Symfony 2.8 (and remains there) which means that it is no longer possible to use non-static closures with the console component.

Simplified example based on some of my code:

public static function register($console)
{
    $console
         ->register('acme:command')
         ->setCode(function (InputInterface $input, OutputInterface $output) {
             self::run($input, $output);
        });
}

public static function run(InputInterface $input, OutputInterface $output)
{
    // ...
}

Results in:

PHP Fatal error:  Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException'
with message 'Warning: Cannot bind an instance to a static closure' in 
/projects/api/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:286

This is caused because with the code introduced in 2.8 it attempts to bind an instance to any closure, which is not permitted for static closures.

I couldn't locate why this code change was introduced in the first place - simply removing the bind call appears to solve my problem, but without understanding why the change was introduced I don't know what else I might be breaking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions