在PHP 5.3的闭包中是否有针对$this的解决方法?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在PHP 5.3的闭包中是否有针对$this的解决方法?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1491字,纯文字阅读大概需要3分钟。
内容图文
我的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);
}
}
);
}
//...
如果info是私有的,则不能,并且您需要升级到php 5.4,因为在PHP 5.3中,闭包中的上下文与对象上下文不同.
内容总结
以上是互联网集市为您收集整理的在PHP 5.3的闭包中是否有针对$this的解决方法?全部内容,希望文章能够帮你解决在PHP 5.3的闭包中是否有针对$this的解决方法?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。