Question

I Searched a lot to find the answer to this question, here on stackoverflow and over on google. But was not able to find anything...

So, my question is: It is a good practice to extend a controller (in my case BaseUserController from zfcuser) once in a module, and once more in another module?

Thanks for all your replies!

Was it helpful?

Solution

It's fine, I would not go as far as "good practice".

Excessive use of inheritance can cause you issues in any language and there are numerous of post around explaining the issues and possible solutions.

From a ZF2 perspective in doing so you will have the issue where Module B depends on Module A which may be a problem - However this really depends your application/module design.

There are other alternatives:

  • Aggregation - create new functionality by taking other classes and combining them into a new class. Attach an common interface to this new class for interoperability with other code.

  • Use PHP traits - If you lucky enough to use the more recent versions of (PHP 5.4+) the you can simply reuse these within each controller class that requires it.

  • A Custom Controller Plugin - ZF2 has a "pluggable" API within the controller, meaning you can write self contained helper classes that can then be used in any controller - No need to extend. You are almost certainly using these already with $this->redirect() or $this->params() so they might be good places to start to see how they are built.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top