I'm trying to configure an object at runtime passing a callback function like so:
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 when not in object context
My question is if there is a way to achieve this behavior using $this
inside a callback function or maybe get a suggestion for a better pattern.
PS: I prefer to use a callback.