Accueil > INFORMATIQUE > PHP > Motifs de Conception > Model View Controller pattern and PHP

Translation of Harry Fuecks article on phpPatterns, 11.21.2002

Model View Controller pattern and PHP

1 - MVC 1 Version

mardi 18 avril 2006, par thierrybo

Toutes les versions de cet article : [English ] [français ]

I write PHP just for my pleasure since several years, it is just a hobby. Without programing background, either to shool or in my jobs, my experience deals only with procedural code. Since one or two years, I started to read about Object Oriented Programing, without make the leap. I was in fact looking for something that could introduce me, and I found in Harry Fuecks articles was I was looking for. I like particularly his articles about MVC Design Pattern.
It was important for me to provide a french translation of this article on my blog, adding original invaluable comments that disapeared when Harry changed his site layout in 2005.

Yo can read the original article and download sample code on Harry Fuecks Web site :

http://www.phppatterns.com/docs/design/archive/model_view_controller_pattern

However, you will find below some of the original comments that disapeared from Harry’s article :


COMMENT LIST

Topic : Author :
Ideas for improvement Captain Proton 24.11.2002

I’ve got a couple of comments on the article..

You have the ProductController class extend the ProductView class. This is not separation of view and controler, since the controller ’Is’ the view (because of the extends relationship). You should have a completely separate View and Controller class, and have the controller return a View like this for example :


$controller = new ProductController(...) ;
$view = $controller->getView() ;
echo $view->display() ;

2) In the MVC design pattern, one view always has one controller, no more than one. In you example code, you use one controller for what are essentially two views : productItem and productTable. You should have two controllers for these items.

3) The purpose of a Controller in MVC in a desktop application is to react to user input such as the user typing text, clicking the mouse, etc. Since PHP is server-side it does not need to control these things. In my opinion, the only purpose of a controller in PHP is for views that have a Form which displays a Model that can be edited. The controller ’reacts to this input’ by calling the appropriate methods on a Model object (such as $obj->saveToDatabase()). In your example, you use the controller to decide which view to display.

4) Another task of the controller is to load the Model objects. In your example, you pass the model object to the controller as a parameter. Since a controller can only be used for one type of Model, this makes no sense.

I know that it’s difficult, if not impossible, to apply the MVC pattern the way it is done in desktop applications because of PHP’s and the web’s (stateless) nature, so for a language like PHP we have to alter the pattern a bit. But if you want to stick to the original MVC pattern, this is certainly not the way to do it. You are on the right track, but I advice you to do some more research on MVC and then go back to the drawing boards :)

Reply
RE : Ideas for improvement Harry Fuecks Harry Fuecks 24.11.2002

No contest :) And many thanks for the feedback ! Will see about a new version of this article in the coming week hopefully.

Reply
RE : Ideas for improvement Harry Fuecks Harry Fuecks 27.11.2002

Further to that, seems in fact I’m mixing up the "Front Controller" pattern with the "Controller".

I’ll be looking to put together something which revises the MVC pattern here and incorporates the idea of front controllers.

Long term, it would be nice to put together a PHP Petstop, given the cruelty to animals online is in fashion fe days in all the leading software houses, which shows how PHP can be used to accomplish end results comparable to Java.

The watch phrase in that is "time permitting..."

P.-S.

Note : Don’t use this example code as a reference. Harry released a second version of this example code (mainly for View and Controller classes) named “MVC2”.

Répondre à cet article | RétroLiens :0


Répondre à cet article