2014-01-23 63 views
0

我的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), 
     ); 
    } 

} 

回答

2

如果信息方法是公共的,你可以這樣做:

//... 
public function fire() 
{ 
    $self = $this; 
    $self->info('Starting chunk'); 
    Item::chunk(1000, function($items) use ($self) 
    { 
     foreach ($items as $item) 
     { 
      $self->info($item->img); 
     } 
    } 
    ); 

} 
//... 


如果信息是私有的,你不能和你需要升級到PHP 5.4,因爲在PHP 5.3閉包中的上下文與對象上下文不一樣。

相關問題