Article or book about MVC?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

blur
Forum Newbie
Posts: 2
Joined: Tue May 15, 2007 9:57 am

Article or book about MVC?

Post by blur »

Hello, I'm new over here. First of all, if this is not the place for this topic, please move it.

The last few weeks I was reading a lot about MVC, mostly articles that I found with google. I think I understand the logic, that of the three layers and all that staff. But I can't implement it, the articles don't have enough examples and don't build a complete MVC application for demonstrate the logic. That's why I'm asking here for more good articles about this topic (even when I think I read them all) or even better, a book.

Thanks in advance and sorry about my english.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I'll be honest, MVC is the both the easiest and most difficult pattern there is. You really cannot learn it from a book, or even by trying to implement it. You really need to have solved various types of N-tier and Presentation layer dependency problems before you can clearly see how to implement MVC. I certainly don't have a full grasp of them as it takes a lot of experience. Just keep at it -- especially the N-tier layer separations.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

The problem with MVC is that it's poorly documented in the literature for PHP. There are so many PHP books, and most take the easy way out using simplistic examples that don't fully demonstrate why it's a valuable pattern. My own advice is a lot simpler - pick three PHP Framework (phpCake, Symfony and the Zend Framework) and read their manuals. All three will reference the three layers (Controller, Model and View) and might show how each works in tandem even though they are separate layers.

P.S. I gather from a previous thread my use of "layer" is questionable. Bad terminology sets in early ;).

It's still worth noting that Frameworks will give you a tool - that doesn't mean they'll explain best practices in MVC. Keep asking questions here as you dig deeper :).
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

From my experience (by no means expert) there are infinite ways to implement MVC in accordance to your own use case and preferences. This makes a definition difficult. There are probably only a couple of things that stay the same in all implementations and then there are some that are frequently the same and other things that are always implemented in different ways.

I have looked fairly extensively at Zend's and glanced at 3 others and none of them suited how I wanted to work. So I'm doing what most seem to end up doing and writing my own.
blur
Forum Newbie
Posts: 2
Joined: Tue May 15, 2007 9:57 am

Post by blur »

Thank you guys for your answers. I will follow your recommendations, and I will take a look at that frameworks to have an idea of how does it work. Then I will try to build my own and ask if i have any question :wink:
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Here is my two bits:

What I have really noticed is that most books or articles on the subject focus on MVC from a event driven paradigm, like traditional desktop application development. Because of this, most articles relating to PHP also convey that same message, despite being a totally different approach to development.

This lead to major confusion for me as I tried to map my understanding of desktop application development and it's various MVC (some might argue more appropriately MVP) frameworks to web based MVC frameworks. There are not huge differences, but there are differences. Especially when refering to dependencies, which is where most articles on MVC get it wrong in talking about web based stuff.

Like most before have said, you really need to learn via trial and error and apply the concepts to your unique problems. It's probably best to take advice from a web developer on how best to implement MVC as they will likely have spotted the caveats and differences unqiue to a request/response cycle.

Have fun :)
User avatar
hongster
Forum Newbie
Posts: 6
Joined: Fri Mar 30, 2007 12:54 am

Post by hongster »

I recommend Tried and True Object Development: Industry-Proven Approaches with UML.

It was written by a group of developers from Nokia. It gives a very good example of software development from analysis to detail design. They are using a modified version of MVC and called it MVC++. In my opinion, i think the book gives a very concrete example and the approaches discussed are quite practical.
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

IMHO, i think MVC is one of the simplest concepts in programming, yet finding it can be complicated because in one application you could be using many languages (PHP/HTML/Javascript/Stored Procs) so people get confused with what is the M the V and the C. I find the best way to understand it is to take it one language at a time. So for PHP

-Model is your functions (classes/methods)
-Controller is the code that is first called (when your page is loaded) that determines what function(s) need to be called
-View is the output that the user sees. Technically it would be the output from your functions, but HTML (and templates) is often included as part of the view.

There is also the Database which is often times confused with being the model. The Database can have its own MVC model as most databases do have their own language.

And there is also Javascript which is its own language and can also have its own MVC (which runs on the persons client). So the output of your PHP code could be Javascript that you have written in an MVC structure
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

IMHO, i think MVC is one of the simplest concepts in programming
Interesting, because it sounds to me like you haven't understood it at all.
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

ole wrote:
IMHO, i think MVC is one of the simplest concepts in programming
Interesting, because it sounds to me like you haven't understood it at all.
No need to attack me like this. If I am wrong I would love for you to explain to me where I made the mistakes. I am constantly learning and if I have it wrong I would like to know where. I have no problem admitting I am wrong.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Sorry that was rather rude.
I have no problem admitting I am wrong
That's a great way to be.
If I am wrong I would love for you to explain to me where I made the mistakes
Certainly.

yet finding it can be complicated because in one application you could be using many languages (PHP/HTML/Javascript/Stored Procs) so people get confused with what is the M the V and the C
Most programming domains involve one or two languages. Web development is slightly unique in that we use many, sometimes as many as six. These can be categorized by what they do
  • Behavioural - these are languages used to define the application layer and thus the behaviour of the application such as PHP and JavaScript,
  • Data Representation - not so much a language more a format, HTML and XML are these
  • Data Retrieval - languages for getting data out of something, examples of which as SQL, SOAP etc.
It is only possible to to implement a MVC in a behavioural language. This means PHP and JS are possible candidates for an MVC. Generally JS is only used minimally so there is no need but one can easily write mountains of PHP so MVC can be a lot of use there.

MVC splits PHP code into its roles and responsibilities and aims to make code more flexible and readable in the process. For me the main advantage to MVC is unit testability as well but I won't confuse you with that.
-Model is your functions (classes/methods)
That is a slight over-simplification as all parts of MVC can involve classes and methods. The model layer is a series of classes for representing data and state. Often people will have a model class for each table in their database. This way they can isolate PHP code that interacts with the database from the rest of the application. Another common model class is a user class used to keep track of user-related state and permissions.
-Controller is the code that is first called (when your page is loaded) that determines what function(s) need to be called
Sort of. The controller is the first point of entry initiated by the user. You can think of controller as handlers for certain user actions. Controllers often link to together models with views and also contain the kind of logic we most commonly associate with application behaviour.
-View is the output that the user sees. Technically it would be the output from your functions, but HTML (and templates) is often included as part of the view.
The view is responsible for translating data sent to it from the controller (originally from the model) into a form which the user can appreciate and interact with. In PHP this involves the generation of HTML. You can pretty much guarantee that if you are writing HTML in your application it should be in the view layer. This doesn't mean that HTML is the view layer because you can easily have a PHP abstraction of HTML for generating it, so that no HTML is visible, and that is still view code.

But there are at least 3 variants on every statement I just made. For instance in some MVC implementations views are allowed to indicate with the model directly whilst in others only the controller can query models and pass it to the model.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

ole wrote:That is a slight over-simplification as all parts of MVC can involve classes and methods. The model layer is a series of classes for representing data and state. Often people will have a model class for each table in their database. This way they can isolate PHP code that interacts with the database from the rest of the application. Another common model class is a user class used to keep track of user-related state and permissions.
Actually, if I have to be pedantic, that's an oversimplification as well. The model is the part of your application, which models the problem domain (Hence the name). You could also think of it as the semantic layer of your application. In a data-centric application, you will group functionality around data entities, and thus end up with something like you describe. However, model layer components doesn't need to have state. For example, interfaces to external services (Such as a web-service), would belong in the model layer.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Thanks for that kyberfabrikken. I wish I had the benefit of reading that before.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

kyberfabrikken wrote:The model is the part of your application, which models the problem domain (Hence the name).
kyberfabrikken describes the Model beautifully.

MVC is an interesting pattern with lots of useful information and good practices, but unfortunately it does not have the kind of cookbook style examples available with other patterns. An interesting thing about MVC is its relationship to 3-Tier architecture -- especially in PHP. MVC and 3-Tier are different way of looking as the same problem. In PHP (and web apps in general) they seem to merge in a nicer way than in previous generation's application types, such as distributed systems or single-user GUIs.

Typically in PHP you have some one or more Datasource Connection objects in the Data Layer -- databases, feeds, the session, etc. On top of that you have the Model (or Models) that are in the Domain/Business Layer. And on top if that you have the View and Controller in the Presentation Layer. So the usual layer dependency goals apply which make the separation between the Model and the View/Controller usually very straightforward.

That leaves where to draw the line between the View and Controller. I think there is general agreement that the View is more about generating the output for the Response. Likewise, I think there is general agreement that the Controller is more about managing Program Flow and the Request. But that is still not a clear division and each application/framework needs draw the line as appropriate.
(#10850)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I'm writing my new site in an MVC style. It's pretty good fun. I've got various "Model" objects (one for articles, another for labels, that sort of thing), I've got a Controller object that provides access to everything, and I've got a View object for each section that draws all the HTML stuff (via simple templates). I found the implementation relatively straightforward. The only difficult bit was handling form inputs because it all goes through the Controller now rather than being dealt with by the script that draws the form like I've done for years. It works.

But...

It doesn't simplify anything. I really can't see the point in it. If anything it makes things more complicated and harder to develop than a simple 'old school' site that doesn't bother with a Controller and just has Model objects and View objects.
Post Reply