laracasts/Commander

Unable to use $this->execute inside an artisan command

Closed this issue · 15 comments

Hi,
i'm trying to use the CommanderTrait inside an artisan command, but i receive the following error:
{"error":{"type":"ErrorException","message":"Declaration of Laracasts\\Commander\\CommanderTrait::execute() should be compatible with Illuminate\\Console\\Command::execute(Symfony\\Component\\Console\\Input\\InputInterface $input, Symfony\\Component\\Console\\Output\\OutputInterface $output)","file":"\/Users\/lucacri\/Sites\/YodaImporter\/app\/commands\/ReassignVncPorts.php","line":77}}

From what i can see, the Illuminate\Console\Command has already an execute method in line 110:
protected function execute(InputInterface $input, OutputInterface $output)

and it's not working well with the CommanderTrait. Any suggestion on how to fix it?

That's because the command class you're extending already has an execute method. So, when you pull in the trait, they're clashing.

I know, I figured it out after a bit of head-scratching :-)

I know it would break backwards-compatibility to change the method name, so I guess we can either close it or maybe create a wrapper trait for artisan commands. I used this package in all my CLI commands and it makes a lot of sense (CLI and Web are both external facing parts of the app). What do you think?

Maybe a more explicit executeCommand function name would be more semantic anyways?

+1 - It wouldn't be backwards compatible, but I also think 'executeCommand' would be a better method name

Does anyone know of a way around it without modifying the CommanderTrait?

@samwalshnz @laracasts @lucacri

http://www.sitepoint.com/using-traits-in-php-5-4/

Use the alias.

use Commander {
    Commander::execute as executeCommand
}

(its bad, but if you want to avoid extend/modify the existing commander, its a solution)

Oh man! You learn something every day. Thank you @stevenklar!

wow. Really nice reference.

@stevenklar ... I'm still getting the same error. Can you paste a code that works?

use Commander { executeCommand insteadof Commander::execute } should be possible too (according to the link provided above).

Unable to use the alias also...

@JeffreyWay, not sure if this means this issue should be reopened or not, but I've just tried to use the code examples @juukie14 and @stevenklar have recommended with no success.

I am having the same issue with the conflicts too

Has anyone solved this?