We have this class
class Fruit {
public $name;
public $color;
function set_name($name) {
$this->name = $name;
//ISSUE:
//We are using a PHP library that accepts a callback function as parameter
new BlaBlaLibrary($this->some_callback);
}
function some_callback() {
......
}
}
$f = new Fruit();
$f->set_name("apple");
As you can see, we are using a PHP library that is accepting a callback function as parameter. We want to do this but doesnt work:
new BlaBlaLibrary($this->some_callback);
This one works just fine though:
new BlaBlaLibrary(function(){
....
});
Question: How can we pass the function of the class and not an anonymous function?
Thanks in advance.
Recognized by PHP Collective
1
You can use
new BlaBlaLibrary([$this, 'some_callback']);
This array tells PHP that you want to use the
some_callback
method of the current object ($this
) as the callback
make sure
some_callback
defined as public
Recognized by PHP Collective