为什么方法实例化后包含$this->info("")的方法会报错?
1. 运行环境
1). 当前使用的 Laravel 版本?
6.20.39
2). 当前使用的 php/php-fpm 版本?
PHP 版本:7.4
2. 问题描述?
在laravel框架的Commands里创建命令行脚本,代码如下:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Test2 extends Command
{
protected $signature = 'test2';
protected $description = '测试方法2';
public function __construct()
{
parent::__construct();
}
public function handle(){
$this->info("hello");
}
}
此时的执行结果是成功的:
# php artisan test2
hello
可是在其他类中实例化引用,$this->info关联方法就会出现问题:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Test extends Command {
protected $signature = 'test';
protected $description = '测试方法';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$t = new Test2();
$t->handle();
}
}
# php artisan test
Symfony\Component\Debug\Exception\FatalThrowableError : Call to a member function writeln() on null
at /www/vendor/laravel/framework/src/Illuminate/Console/Concerns/InteractsWithIO.php:264
260| public function line($string, $style = null, $verbosity = null)
261| {
262| $styled = $style ? "<$style>$string</$style>" : $string;
263|
> 264| $this->output->writeln($styled, $this->parseVerbosity($verbosity));
265| }
266|
267| /**
268| * Write a string as comment output.
Exception trace:
1 Illuminate\Console\Command::line("hello", "info")
/www/vendor/laravel/framework/src/Illuminate/Console/Concerns/InteractsWithIO.php:249
2 Illuminate\Console\Command::info("hello")
/www/app/Console/Commands/Test2.php:25
Please use the argument -v to see more details.
测试后发现,只要包含$this->info(“”); 相关打印,脚本就会停止。
不明白这是为什么
推荐文章: