次のようにコールバック関数を渡して、実行時にオブジェクトを構成しようとしています。
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';
});
もちろん、次のエラーが表示されます。
致命的なエラー: オブジェクト コンテキストでない場合に $this を使用する
私の質問は、コールバック関数内を使用してこの動作を実現する方法があるかどうか、$this
またはより良いパターンの提案を得る方法があるかどうかです。
PS: コールバックを使用することを好みます。