I am trying to set up an object at runtime by passing a callback function as follows:
class myObject{
protected $property;
protected $anotherProperty;
public function configure($callback){
if(is_callable($callback)){
$callback();
}
}
}
$myObject = new myObject();
$myObject->configure(function(){
$this->property = 'value';
$this->anotherProperty = 'anotherValue';
});
Of course, I get the following error:
Fatal error: using $ this if not in object context
My question is, is there a way to achieve this behavior using $thisthe callback functions inside, or perhaps get an offer for a better template.
PS: I prefer to use a callback.
source
share