vendredi 17 septembre 2021

PHP Class inheritance

Suppose I have the following :

<?php
class Final extends Intermediate {
  public function final_level() {
      $this->low_level();
      $this->inter_level();
  }
}

class Intermediate extends Lib1 {
  public function inter_level() {
      $this->low_level();
  }
}

class Lib1 {
  public function low_level1();
  public function low_level2();
}

class Lib2 {
  public function low_level1();
  public function low_level2();
}

I would like to change the Intermediate class to extend Lib1 or Lib2, depending on some conditions, without duplicating Intermediate and Final code content.

All low_level functions are the same for both Lib.

In the end, I would like to have a Final1 class that use Lib1 (and Final2 that use Lib2).

How could I achieve this ?

Aucun commentaire:

Enregistrer un commentaire