[英]In laravel 5, how to access to Command class methods from outside classes?
我正在开发一个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);
}
....
}
我希望方法可以静态访问,如:Command :: error(“some error”);
但也许这不是预期的用途?
我建议您使用“ 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.
在app \\ Console \\ Kernel.php中
$schedule->command('mycommand')
->everyTenMinutes()
->sendOutputTo(storage_path('logs/mycommand.log'))
->name('mycommand')
->withoutOverlapping();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.