Marko Vasic
Marko Vasic

Reputation: 690

Pass method from controller as callback to another

I have something like this:

public function generateAudio(){

$sourceProcessor = new Sourceprocessor($_SESSION['input_file']);
$sourceProcessor->extractAudio();

}

public function checkProgress($percent){
  //do something with a percents
}  

and in SourceProcessor I have something like this:

public function extractAudio($callback=NULL, $folder=NULL){

   if(is_callable($callback)){
       $callback($percentage);
   }

}

I tried to pass method like

$sourceProcessor->extractAudio(array($this,'checkProgress');
$sourceProcessor->extractAudio(array($this->checkProgress());

but nothing seems to work. Anybody have any idea how to do this?

Upvotes: 1

Views: 63

Answers (1)

Jeff Lambert
Jeff Lambert

Reputation: 24671

Call your method like this:

$sourceProcessor->extractAudio(array($this,'checkProgress'));

And call the callback from extractAudio like this:

call_user_func($callback, $percent);

See the manual entry for call_user_func and callable for more information

Upvotes: 3

Related Questions