Simulating multiple inheritance in PHP

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

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

Both "Backend_Crud_Controller" and "Frontend_Crud_Controller" have the same functionality, and therefore they must extend another class called "Base_Crud_Controller", the only difference comes from the "Backend / Frontend" controllers, which implement different mechanisms.

Mostly they should inherit both classes, but my problem is that the controller "Backend / Frontend" does not necessarily extend the "Base_Crud_Controller".

I know that multiple inheritance does not exist in PHP, but I am looking for a solution, I decided to refrain from Mixins (as in Symfony), since I do not consider this an elegant solution.

Interfaces do not suit me, since they all end with specific classes that must implement the methods.

+3
source share
1 answer

Consider using Decorators or rethinking 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 BackEnd and Crud Decorator and put them together, for example

$crudBackEndController = new Crud(new BackEnd(new BaseController));
+3
source

Source: https://habr.com/ru/post/1743809/


All Articles