Page 1 of 1
MVC and PHP
Posted: Fri Feb 12, 2010 5:17 pm
by kiwi_nz
I notice that MVC - (Model, View, Controller) paradigm has become increasing popular in the last few years. I know there is one of these for PHP as well.
http://cakephp.org/
I also note that alot of well established PHP applications do not follow this. (eg Drupal).
I guess I have a couple of questions.
- Does PHP best practice have its own paradigm?
- Why do so many well established PHP projects mix business and presentation logic?
Would be interested in your thoughts?
regards
Kiwi_NZ
Re: MVC and PHP
Posted: Fri Feb 12, 2010 5:32 pm
by Eran
There are many frameworks besides cakePHP that are based on the MVC pattern - such as the Zend Framework, Symfony and Prado to name a few.
Does PHP best practice have its own paradigm?
this question makes no sense. what are you asking?
Why do so many well established PHP projects mix business and presentation logic?
Many popular PHP projects are developed by amateurs. A very large portion of the community that uses it are amateurs as well. Most amateurs are content with "stuff that works" and are not well acquainted with "best practices".
Re: MVC and PHP
Posted: Fri Feb 12, 2010 7:15 pm
by alex.barylski
Many popular PHP projects are developed by amateurs. A very large portion of the community that uses it are amateurs as well. Most amateurs are content with "stuff that works" and are not well acquainted with "best practices".
+1
Re: MVC and PHP
Posted: Fri Feb 12, 2010 8:08 pm
by Christopher
kiwi_nz wrote:I notice that MVC - (Model, View, Controller) paradigm has become increasing popular in the last few years. I know there is one of these for PHP as well.
http://cakephp.org/
I think you might be confusing whether MVC is a popular discussion subject and framework feature checkbox, or whether it is actually widely used in implementations.
kiwi_nz wrote:I also note that alot of well established PHP applications do not follow this. (eg Drupal).
As you noted, MVC has only been widely known and implemented in the last few years. You will probably see the next generation of PHP apps making more use of MVC and frameworks.
And, I think there is some truth to pytrin's comment about amateurs starting and building many popular PHP applications. However I don't think this is a negative. In fact I have always said that this is PHP's greatest strength. Some guy in a college dorm room can have an idea of keeping friends in touch -- and with PHP can create Facebook. Unlike high-brow, computer-sciencey lanugages, PHP makes implementing interesting web ideas easier. And I am sure that the Facebook code quality has improved since then.
kiwi_nz wrote:Does PHP best practice have its own paradigm?
It has a bunch of them. Many are standard computer science practices, others such as the use of APC or how scripts are organized are specific to PHP.
kiwi_nz wrote:Why do so many well established PHP projects mix business and presentation logic?
I want to be clear that you are talking about 2-Tier and not MVC with that question. And I want to point out that mixing business and presentation logic is the single biggest problem in all of programming. So you are asking why the biggest problem in programming ... is also the biggest problem in PHP?
kiwi_nz wrote:Would be interested in your thoughts
Sorry ... anyone around here can tell you that I refuse to get dragged into MVC discussions because I don't have strong opinions about it!

Re: MVC and PHP
Posted: Fri Feb 12, 2010 8:16 pm
by Eran
However I don't think this is a negative
Neither do I, just describing what I see. There is much use for amateur developed FOSS tools, no doubt
Re: MVC and PHP
Posted: Fri Feb 12, 2010 8:21 pm
by Christopher
pytrin wrote:Neither do I, just describing what I see. There is much use for amateur developed FOSS tools, no doubt
Right, but my point is that the guy who started Drupal looked around and found the code that worked the best for him at that time. If he started to day there is a much greater chance that he would use MVC and/or a framework because those are now widely practiced and discussed.
You can blame amateurs for having great ideas. Nor blame them for using the most accessible code available them at the time.
Re: MVC and PHP
Posted: Fri Feb 12, 2010 9:22 pm
by kiwi_nz
arborint wrote:
kiwi_nz wrote:Would be interested in your thoughts
Sorry ... anyone around here can tell you that I refuse to get dragged into MVC discussions because I don't have strong opinions about it!

I guess this is the more salient point. The MVC is a pattern that is not wholeheartedly embraced bye everyone. I guess it comes down to maintainability.
My background is not in PHP but rather in Django/Python. However what I have noticed about PHP is that trying to understand someone else's code base and I am looking at a popular PHP project and I see they have made no attempt to separate presentation from business logic which I guess is more the salient point rather than whether it is MVC or not.
Perhaps I should have titled this PHP/CGI vs other ways of doing PHP. I understand the PHP has ways of embedding html within .html templates? Looking at my current PHP project I see they have no templates but everything is dynamically constructed. Ugggh. Not at all the way I am used to working. Is CGI still very popular or is it being superseded in favour of some other PHP framework??
Re: MVC and PHP
Posted: Fri Feb 12, 2010 10:23 pm
by Christopher
kiwi_nz wrote:I guess this is the more salient point. The MVC is a pattern that is not wholeheartedly embraced bye everyone. I guess it comes down to maintainability.
Right. And MVC is not the only way to produce maintainable code. And maintainability is not the only criteria in deciding on an architecture. I think it is the best solution, but I naturally tend to build my Model or View code first and my Controller code last. That make MVC work better for me than for people who tend to write Controller code first and then build out.
kiwi_nz wrote:My background is not in PHP but rather in Django/Python. However what I have noticed about PHP is that trying to understand someone else's code base and I am looking at a popular PHP project and I see they have made no attempt to separate presentation from business logic which I guess is more the salient point rather than whether it is MVC or not.
Yes, exactly. And I agree with you on the importance of that layer separation. That's how I build my apps. However, if you are willing to accept the maintenance and growth problems, then writing Transaction Scripts can be acceptable especially in web apps. The reason is because the business logic is usually fairly simple and is focused on quickly generating some simple data to build a web page. Given how PHP can be broken up into pages, the result is a reasonably organized and readable application for most websites. I think there is a gap between what we might like and what is actually bad.
kiwi_nz wrote:Perhaps I should have titled this PHP/CGI vs other ways of doing PHP. I understand the PHP has ways of embedding html within .html templates? Looking at my current PHP project I see they have no templates but everything is dynamically constructed. Ugggh. Not at all the way I am used to working. Is CGI still very popular or is it being superseded in favour of some other PHP framework??
First, one way that PHP is different from other languages is that it is both a templating language and an application programming language. This is due to PHP being embedded in the web server, unlike regular languages. PHP programmers keep the difference straight in their heads, so where you see a mix of domain and presentation there may actually be clear dependencies. And it is the dependencies that are most important -- not just that the code is structured in a way you like.
You should take a look at one of the ports of Django templates to PHP:
http://wiki.github.com/speedmax/h2o-php/
http://www.beberlei.de/calypso/
I think my bigger question is: What is your question? Are you really interested in opinions about PHP architecture? Do you just want to whinge about how crappy you think the code is in some PHP projects? Or do you want to be able to write Django style code in PHP? Something else?
Re: MVC and PHP
Posted: Fri Feb 12, 2010 10:49 pm
by kiwi_nz
Thanks, I will have a look at those links as soon as I get a chance.
arborint wrote:
I think my bigger question is: What is your question? Are you really interested in opinions about PHP architecture? Do you just want to whinge about how crappy you think the code is in some PHP projects? Or do you want to be able to write Django style code in PHP? Something else?
Well whinging doesn't really accomplish anything does it. I don't really think anyone is going to totally reorganize their code base, irrespective of the strength of my case!!

Most of the time, Really, in my opinion, it comes down to the community culture. For instance are they helpful or disparaging of newcomers. This, while being slightly fuzzy concept, has an impact on how maintainable or adaptable a project is for someone previously unrelated to the project.
The reason I brought up Django/PHP was not so much for odious comparisons but rather because I have two projects I am trying to integrate together. One written in Django and another written in PHP. (Perhaps the details should really be a subject of a different post.) There are alot of "good looking" PHP projects out there. It seems silly to reinvent the wheel if the two can be made to work together.
Re: MVC and PHP
Posted: Sat Feb 13, 2010 1:37 am
by Christopher
kiwi_nz wrote:The reason I brought up Django/PHP was not so much for odious comparisons but rather because I have two projects I am trying to integrate together. One written in Django and another written in PHP. (Perhaps the details should really be a subject of a different post.) There are alot of "good looking" PHP projects out there. It seems silly to reinvent the wheel if the two can be made to work together.
I think there are a lot of interesting ways that you could make your work in Django and PHP overlap to give you some development/maintenance benefits. I mentioned using Django compatible template in PHP would be pretty easy and mean you only had one template style across projects. Obviously you should standardize on the client side Javascript and CSS implementations. Those two things would level the playing field for the design work.
Then you would probably want to start working from outside in to extend the consistency. For example, Horde Routes implements a routing library that is the same in Python and PHP. That would make all of your URLs consistent both externally and internally. A next step might be to make the Request/Response cycle similar. Maybe make the Ajax calls compatible in PHP to how Django handles them. Build the Models in parallel, and perhaps implement some web services so the two systems can talk to each other. Etc., etc., etc.