mercredi 24 mai 2017

Common methods and different constructors

I am working in custom MVC php web application. Here is what I have at this moment:

  • A class "CtrlBase" which all controllers should extend.
  • And because the application have 3 different parts (applications (I will use the word applications here)) that are independent from the user's point of view, but a lot of functionality is shared between the 3 apps I have a specific controller for each app that extends "CtrlBase": CtrlBaseAdmin, CtrlBaseCms, CtrlBaseWeb.

    • The main difference between the 3 different controllers that should be taken into account at this level is in the constructor.
    • I have a CRUD controller for users that has the same functionality in all 3 apps except that each extends the specific base controller depending on the app. This example code will illustrate it better:

    class Users extends CtrlBaseAdmin {

    public function __construct() {
        parent::__construct();
    }
    
    public function index()
    {
        .
        .
        .
    }
    
    public function edit()
    {
        .
        .
        .
    }
    .
    .
    .
    
    

    }

All the methods: index, edit, add, and delete are the same for all three apps but the parent constructor id different and all the controllers of the same app extend the same parent controller.

So my question is how to avoid this code duplication?

Thank you

Aucun commentaire:

Enregistrer un commentaire