0

I've got a problem I haven't been able to get my head around. I'm working on my own web app framework in php. I know there's plenty of good stuff out there but I like to play around with this and I'm not using it for any sensitive production stuff.

So here's the problem: I have an abstract base class which contains some functions that I want all of the models to inherit. There's also an abstract base controller which extends the base model with functions for basic manupulation. Currently, all created models extend the base model and all controllers extend the base controller. So, all controllers also inherit from the Base model through the Base controller. But now I have no access to model properties from the model controller since models extend the Base controller and it seems weird for models to extend the Base controller.

How should I set this so that I can have access to the model properties from their respective controller?

Thanks!

onomio
  • 361
  • 1
  • 11
  • 1
    Why exactly do you need for `BaseController` to extend `BaseModel` ? What problem are you solving this way ? – tereško Jun 10 '12 at 19:30
  • BaseModel defines functions like __set and __get. BaseController has actions like getting data to send to a view. I guess all function could go into a single BaseClass but since what is in BaseController are manipulations of data and what is in BaseClass concerns the properties it seems logical to seperate them. – onomio Jun 10 '12 at 19:49
  • I fail to see why controller would need magic getters and setters. Also, you should avoid them. Magic getters/setters come with performance penalty. Other problem would be - how would you enforce that `$something->email` **cannot** contains invalid email address ? – tereško Jun 10 '12 at 19:55
  • The validation conditions are set in the model, the controller checks to see whether there are conditions and then validates the data. If it fails the action is aborted. – onomio Jun 10 '12 at 20:11

1 Answers1

2
  • To begin with, it would seem to me, that your understanding of what is Model .. well .. is bad. Maybe this comment would help a bit.

  • The other thing, which seems wrong which whole picture is this:

    So, all controllers also inherit from the Base model through the Base controller.

    You should avoid deep inheritance. And since Controllers and What-You-Call-Models have completely different responsibilities, there should nothing inherited by both. You must understand that extends is a synonymous with is-a.

    If you write class Admin extends User, then it means that every admin is a user. This also means that, when you write class User extends Table, it is wrong (.. and kinda insulting).

  • Controller should not read properties from what-you-call-model (assuming you are not implementing MVP pattern instead). That part should be done by View, since that would be the part, responsible for presentation logic.

    And to for that you what-you-call-model should have specific getters. You should never exposed properties directly (as in, setting them public), because then you would be breaking encapsulation.

.. my 3 cents

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Well I guess my interpretation of MVC wasn't exactly right. As I understand from the links you posted MVC actually doesn't really exist in web dev anyway. My models hold a mapping of the DB tables and holds the data loaded from the DB, the controllers hold all the actions for the data and the views handle the templates. – onomio Jun 10 '12 at 20:01
  • @Abel , how did you manage to get impression that MVC is not used? I just wrote that [classical MVC](http://www.itu.dk/courses/VOP/E2005/VOP2005E/8_mvc_krasner_and_pope.pdf) (as defined in 80s) is not web-friendly. Which is why there exist 4 different MVC-like patterns which **are** used. – tereško Jun 10 '12 at 20:14
  • Well, I was just referring to this statement: "The secret is : it is impossible to write MVC in php." I guess, in my example I'm trying to achieve Model2 MVC. – onomio Jun 10 '12 at 20:29