3
votes

I am working on my own MVC framework and found myself stuck. I need the following construction:

 Controller  
      --> Backend_Controller
           --> Backend_Crud_Controller
       --> Frontend_Controller
           --> Frontend_Crud_Controller

Both 'Backend_Crud_Controller' and 'Frontend_Crud_Controller' have the same functionality and thus they should extend another class named 'Base_Crud_Controller', the only difference comes from the 'Backend/Frontend' Controllers which implement different mechanisms.

Basically they should inherit both classes but my problem is that 'Backend/Frontend' controller doesn't necessarily extend 'Base_Crud_Controller'.

I know multiple inheritance doesn't exist in PHP but I am looking for a solution, I choose to refrain Mixins (like in Symfony) as I don't consider that an elegant solution.

Interfaces do not suit me as all of these end up as concrete classes that should implement methods.

1
Could you please tell us what each of this classes do? Why did you decide to create such inheritance mode? I've got bad feelings about what you're trying to do. - Crozin
The 'Controller' represents basic controller functionality as in the MVC world. Backend/Frontend controller both extend 'Controller' and implement different logic. In both the Frontend/Backend controllers I could have CRUD_Controllers which implement basic CRUD functionality but note that not all Frontend/Backend controllers need to implement CRUD. - fabieno

1 Answers

3
votes

Consider using Decorators or rethinking your design.

class FrontEnd
{
    protected $baseController;
    public function __construct(BaseController $controller) { /* ... */}
    // ... 
    // methods specific to Frontend
    // ...
    public function __call($method, args) {
        // implement __call to delegate other methods to BaseController
    }
}

You can also create a BackEnd, and Crud Decorator and stack these together, e.g.

$crudBackEndController = new Crud(new BackEnd(new BaseController));