开发者

Imitating Multiple Inheritance in PHP

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.


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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜