2

In web MVC most designs I've seen, (just choosing model layer as an example) the domain problem class, say students, always inherits the superclass "Model." The name is irrelevant, but why do they always inherit classes instead of interfaces?

I've seen others that have interface -> abstract class -> concrete class. But, I don't know why I'd choose a class over an inteface.

  • Will I need to execute something in the constructor of the superclass in MVC? Some other method?
  • I know of no mutual functionality that needs an implementation beforehand, but that is probably inexperience? I don't now.
  • Why not just an interface to enforce my API?

I have seen other answers. I have not seen it in the specific context of MVC design, however.

Using PHP if it matters.

2
  • The most likely reason is that there is some common functionality in the base class. Commented Sep 2, 2014 at 15:12
  • The arrangement you describe is the Active record pattern. It's worth noting that there is also the Data mapper pattern which avoids you needing any common base class or interface on your domain classes.
    – paj28
    Commented Nov 26, 2014 at 10:32

1 Answer 1

1

Let's explore the differences between using an interface vs a base class:

If I'm using an interface, that means I need to also implement the, for example, connection logic for each class which wants to connect to the DB. That is a huge pain.

However, if I just subclass a base class (which already provides connection method), then I don't need to keep on implementing it myself. I can just focus on the specialized behavior of the class I'm writing.

So, in cases like this, using a base class over an interface saves me from repeating myself ad nauseum.

3
  • Does that mean in the constructor of your concrete you do parent::__construct();, so you don't have to keep typing in $db = new PDO(....) or mysqli...?
    – johnny
    Commented Sep 2, 2014 at 15:17
  • I'm unsure about PHP, so I can't answer that. I was answering it from the context of MVC design. Commented Sep 2, 2014 at 15:22
  • I guess I could say calling super, from language X.
    – johnny
    Commented Sep 2, 2014 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.