An alternative to Poor Man’s MVC in PHP

April 7, 2010
By SeanJA

As written in my previous post ( http://blog.seanja.com/2010/03/re-top-10-php-techniques-that-will-save-you-time-and-effort/ ) I noted that the code for the proper way to write an index file is essentially a Poor Man’s MVC tm.

This method is fine for someone starting out their php journey, it lets them do everything they will probably need to do (they can even add in some apache rewrite rules to make the url look nice). The problem with this approach is that, while it is really fast, it is relying on you to define every single page that you add to your site individually by hand (well, except for the last example). Another problem is that you will inevitably end up with something like this (ok, this might be a bit of an extreme example…):

because the next person to edit the code is too lazy to look at how it actually works. This will repeat itself because the next person after that will figure that is how the code works (ad infinitum).

The alternative is to use an actual MVC pattern, there are lots of tutorials out there to help you reinvent the wheel (which is a great learning tool), or you can start with one that is pre built like CodeIgniter, its cousin Kohana, or one of a pile of others.

It is however, up to you to decide the best way forward for your application, you can use of of the Poor Man’s MVC tm pattern if you really want. If you use it properly, with self control, you will have no problems. It is good to look at other options though, so you can actually understand the advantages and disadvantages of other methods. I find that I like the patterns and organization that are ‘forced’ upon me when I use an MVC framework. It means that I know where the code to control specific actions is, the code for the database/object interactions is over there, and the html (views) is in this spot over here. It makes for easy maintenance, and it makes for faster coding when you know exactly where to go.

Share and Enjoy:
  • Twitter
  • DZone
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • HackerNews
  • Technorati
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Yahoo! Buzz
  • LinkedIn
  • Facebook
  • Print
  • PDF
  • email

Related posts:

  1. RE: Top 10 PHP Techniques That Will Save You Time and Effort
  2. Damn-it PHP
  3. Getting Started With PHPUnit
  4. Dynamic Images with PHP
  5. PHP DateTime is missing methods in 5.2

6 Responses to An alternative to Poor Man’s MVC in PHP

  1. Steve on May 27, 2010 at 11:31 pm

    You should also point out that the first example is wide open to remote file inclusion attacks as it takes an external variable and doesn’t sanitize it before use.

  2. John Conde on April 16, 2010 at 2:29 pm

    You should also point out that the first example is wide open to remote file inclusion attacks as it takes an external variable and doesn’t sanitize it before use.

  3. Marcus Kielly on April 16, 2010 at 2:34 am

    Personally, I think you would be MUCH better off using a Page Controller if you haven’t got the time/inclination to roll your own/deploy off the shelf MVC. All you need is a bit of dynamic URL interpretation to load the relevant class.

    • SeanJA on April 17, 2010 at 8:15 am

      I am curious as to what you mean by a “page controller”?

      • SeanJA on April 17, 2010 at 8:28 am

        That is a “page controller” that does not implement the full mvc pattern?

Leave a Reply

Your email address will not be published. Required fields are marked *

*