在laravel 5中,如何从外部类访问Command类方法?

koa*_*aok 5 php debugging console command-line laravel-5

我正在开发一个Illuminate\Console\Command.使用php artisan通过cli运行.此Command类正在使用其他类.我很欣赏Command-> info(),Command-> error(),方法......我怎样才能在依赖中使用它们?

到现在为止,我将其作为参数传递给其他类$ this

例如

class MyClass extends Command {
....
    $g = new MyOtherClass($this, $param...);
    $g->find();
....
}

class MyOtherClass {
$command;
....
    public function __construct($command){
        $this->command=$command;
    }
    public function find(){
        if($error)
             $this->command->error($error);
    }
....
}
Run Code Online (Sandbox Code Playgroud)

我希望方法可以静态访问,如:Command :: error("some error");

但也许这不是预期的用途?

Raf*_*lQm 1

我建议您使用“ echo ”来返回“ $this->command->error ”,因为您可以在内核中使用并保存在不同的日志中,如下所示:

echo '['.date('Y-m-d H:i:s').'] local.ERROR: '.$error.PHP_EOL; // This way it will be better visible in log viewer.
Run Code Online (Sandbox Code Playgroud)

并在 app\Console\Kernel.php 中

$schedule->command('mycommand')
            ->everyTenMinutes()
            ->sendOutputTo(storage_path('logs/mycommand.log'))
            ->name('mycommand')
            ->withoutOverlapping();
Run Code Online (Sandbox Code Playgroud)