我的IDE警告我,PHP 5.4之前的闭包中不允许$this.有没有从5.3.10升级PHP的解决方法?请参阅下面的fire()方法:
<?PHP
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\Inputoption;
use Symfony\Component\Console\Input\InputArgument;
class UpdateItemImageSizes extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'namespace:updateimagesizes';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Updates image size information in the items table.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->info('Starting chunk');
Item::chunk(1000, function($items)
{
foreach ($items as $item)
{
$this->info($item->img);
}
}
);
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
//array('example', InputArgument::required, 'An example argument.'),
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getoptions()
{
return array(
array('example', null, Inputoption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
解决方法:
如果info方法是公开的,则可以执行以下操作:
//...
public function fire()
{
$self = $this;
$self->info('Starting chunk');
Item::chunk(1000, function($items) use ($self)
{
foreach ($items as $item)
{
$self->info($item->img);
}
}
);
}
//...