CakePHP - MVC through PHP!
PHP
PHP is a scripted programming language, primarily used for websites. It's been around for a while, and continues to grow more robust with each passing year. The major downside to it is that it is painfully simple to write a program in it - a monkey could do it! And, in some cases, it appears as though a monkey has. Programmers have a concept called 'Spaghetti Code', where nothing is simple because it's all tangled up. Change one line and it'll ripple through your entire program.
Progress much beyond a simple 'Hello World' type program, and you will discover that there are nearly as many ways to accomplish the same end result as there are programmers. What is needed is a standard coding convention. Enter Model / View / Controller, or MVC.
MVC (Model / View / Controller)
MVC is a design pattern. Simply put, it's a way of expressing and organizing a concept in such a way that a program is broken down into three significant areas: Model, View and Controller.
The Model encapsulates the data itself. Basically, it's like a model is a blueprint of your data, which can then be made every time it's needed. It contains variables for all your data, of whatever format you specify, as well as accessors and the data functionality, like saving and deleting a data record.
The View is the actual end-result, which may be a formatted HTML page, a web service feed (like an RSS or ATOM feed), or any other final product output from your data.
The Controller asks the Model for a data set (for example a record or set of records), makes any changes or actions on that data, then tells the View what to send, whether it be a pretty HTML page, or a blinking HTML error message.
Huh?
An easy way of understanding MVC, that many people have used pretty often, is a spreadsheet. You, the originator of the data, key it in to a spreadsheet file and save it. That represents your data set, and the spreadsheet knows how to organize the data internally, how to save itself, how to delete itself, whatever it needs. That spreadsheet encapsulates your data in a Model.
Say that spreadsheet you've made stores names and addresses. You may want to generate form letters from that at a later time, knowing what fields you've saved in your spreadsheet. The final output, the form letter, represents the View.
So how do you get the information from the spreadsheet into your word processor, formatted how you'd like it? Simple. Macros! And yes, that macro represents a simple Controller, munging the data it got from your Model and reprocessing it, then sending it out into your View.
Sort of makes you wonder - have you been using design patterns and not even knowing it? Likely. Any means of standardizing a process can usually be represented as a design pattern. MVC is just one, and a very powerful one.
OK, So...
So, I've been learning MVC and object encapsulation, through a package called CakePHP, available from cakephp.org. It's an open-source package that creates a set of basic rules to adhere to, and in return, gives you some extremely powerful functionality. There are other MVC frameworks out there, notably Ruby on Rails or Symfony, but CakePHP has the advantage of being based in PHP (Rails requires learning Ruby, an easy language, but another one to learn), and of being compatible with either PHP 4 or PHP 5 (most MVC packages written for PHP are based in PHP 5, because the object orientation is much stronger). The big advantage to CakePHP is that it'll run, out of the box, on pretty much any machine anywhere. I've successfully used it on OS9, OSX, Windows (with either Apache or IIS), Linux... It just works!
As I start new projects in CakePHP, and I have a few in the planning stages, I'll be posting details and samples here. Keep watching!