PHP is famous for it’s flexibility and almost equally famous for the disaster that flexibility can bring. I learnt PHP some time ago from a book which had no qualms about having MySQL queries thrown in with some HTML and numerous echo statements.
It has to be said though the book was about PHP, to push a certain approach to project layout would be arguably the wrong path in its self. At the end of the day, it’s up to the developer to enforce structure upon themselves when working with PHP. This of course is the problem, many don’t bother or are simply not sure which path to take.
I myself only created a simple guestbook application. This was enough to show me the danger though. Enough so in fact that I scrapped all the code and started again. The spaghetti like structure was hard to work with and making changes invariably often broke something. With my clean slate I introduced an object orientated approach. I was happy, it was so much cleaner. My objects in the database related to PHP classes and I’d taken the time to introduce many little helper classes facilitating features such as automatic sticky forms.
There was an issue of development time though, it took a great deal of time to produce all this clean code. The more I wrote the more I felt I was wasting time implementing general functionality to tidy PHP up. This can be problem when trying to follow software development principles using PHP: the more you write in hope of producing the perfect code base, the more you come to the realization you’re essentially creating your own framework.
With this in mind it’s natural to look around and see what existing frameworks are on offer. After all, despite all your efforts you are essentially reinventing the wheel only to find every step you take there’s another spoke missing.
Wanting to play around with a framework and get some code together, I went looking for what was on offer. After a good look around, I’d narrowed it down to three: CodeIgniter, Zend and CakePHP. All of these follow the Model View Controller pattern.
Now I won’t pretend that I did in depth research into which one was best. Arguably that decision should be made on a per-project basis anyway. CakePHP is the one that really caught my attention though. It grabbed me right after I created a few tables in my database and typed the magic ‘cake bake all’ command. Suddenly I had CRUD (Create, Read, Update, Delete) operations for all my entities, based on the tables. But there is more, it is also clever enough to create views attached to these controller actions. This leaves you with a fully functional skeleton application… and you’ve barely lifted a finger. This automated method of code creation is known as ‘baking’ in CakePHP. On top of all this command line awesomeness is the vast API which provides helpers for many common tasks such as authentication, email, Ajax and form building.
Naturally those wanting to learn PHP should stay away from the frameworks before they take the leap into having half the work done for them. For any PHP developer though, frameworks such as CakePHP are a real blessing. After all, why spend time making everything when you can just bake it?