はあなたが親クラスを拡張し、いくつかの子供を持つそして、親クラスPHPベストプラクティス:拡張親機能
class Parent {
function doSomething() {
$this->operationA();
$this->operationB();
}
}
があるとします。
class ChildOne extends Parent {
function doSomething() {
$this->myOperationC();
$this->operationA();
$this->operationB();
$this->myOperationD();
}
}
class ChildTwo extends Parent {
function doSomething() {
$this->myOperationE();
$this->operationA();
$this->operationB();
$this->myOperationF();
}
}
同様のワークフローを設計するベストプラクティスは何ですか?
私はコードの繰り返しを避けたいです(operationAとoperationBはすべての子にあります)。同時に、すべての子が必要とするため、このメソッド(Parent-> doSomething()のみを残して、子メソッドを削除)を拡張できません。いくつかのカスタム操作を実行します。
どうすればこのコードをきれいにできますか?
ありがとうございます。
質問をもっと細かく分解することができれば、すでにあるよりも多くの回答が得られるかもしれません。 **ちょっと考え** – cookies
私がやることは、operationAとoperationBを父の上で保護されたものとして定義し、その後、息子から呼び出す必要があるときにparent :: operationA();を使用することです。 –