Back to the blog
Recent Posts
-
Apr 27
Thoughts on Android and the HTC Dream
-
Feb 26
Announcing Twitterscribe: archive your tweets
-
Jan 01
Adventures in PHP interfaces
-
Oct 17
How do you mockup websites?
-
Oct 09
Something's not right with Android's UI
-
Oct 06
My lance is free
Most Popular Posts
-
Why you should be using a framework
-
Perhaps the world doesn't need another framework
-
Rewriting URLs with Apache's mod_rewrite and PHP
-
Five easy things that make you a better web developer
About the Blog

I'm a web application developer in Melbourne, Australia. If you find anything useful, leave me a comment, and if you need web design, development, or accessibility and usability consulting, contact me! Cheers.
Twitter: joshsharp
Why you should be using a framework
Saturday 13 Oct, 2007 09:14 PM
There's a reason why PHP is growing rapidly as a server-side scripting language — it's very easy to pick up. Many functions are included without needing any sort of namespace importing, and you don't even have to write OO code if you don't want to. Variables are weakly typed and the syntax is fairly familiar.
But PHP's ease of use is also its downfall. Because there are less restrictions on the structure of the code you write, it's much easier to write bad code. But there is a solution: use a framework.
PHP frameworks like CakePHP, CodeIgniter and the Zend Framework (which I wasn't too taken with) provide a solid structure for your code whilst also offering some extra functionality that would be much harder to replicate on its own. It's important to note, too, the frameworks mentioned follow the MVC pattern, which is fairly common and what I'll talk about below — some of these benefits will apply to other patterns as well, but not all.
Of course, if you're not taken with any of the packages above you can also write your own framework, which I've done and will talk about in the next few posts. But for now, let's have a look at the common benefits of a framework.
MVC structured code
Model-View-Container is a widely recognised design pattern that separates out database and business logic from the presentation layer (in this case, the (X)HTML). Separation of presentation and logic aids in maintainability — clean code is easy, understandable code. It means you're no longer searching for that elusive function returning you an empty array — so it comes from the database? It must be in the model. On the flip side, it also means you can update the look and feel of a site without having to worry about wading through PHP script tags. It's like the server-side equivalent of CSS.
Enforcing of good coding standards
Another effect of MVC code is that it makes it so much easier to code as you should. For example, in my framework Rex, the Model is covered by two abstract classes — DataObjects and DataHandlers. DataHandlers instantiate and manipulate DataObjects, which are basically big associative arrays wrapped lovingly in getters and setters. But here's the neat bit — DataHandlers are the only object which can access the database directly. Of course, you could get around this if you wanted to, but by sticking to rules like these, you save yourself months of headache. If your User records are failing to insert, well no contest — it's an issue with UserHandler. Too easy.
Pretty URLS
Most of these frameworks use a bit of mod_rewrite magic to ensure your URLS are always clean and accessible. Apache handles requests to a particular URL, and (hidden to the client) returns this URL to /index.php as a GET string. The index page instantiates your main framework object (in Rex's case, the App object) and farms the query off, and you're done. But all the end-user knows is that they're at /users/browse/page/2 — they don't ever even know you're using PHP, for that matter!
Accessible URLS also help with SEO — linking to your blog post as /blog/view/why_you_should_be_using_a_framework/ is going to boost your search engine ranking for those keywords more than /blog/view.php?id=12 ever could.
Helpful helpers
Remember how every page query starts from index.php? I promise this will make your life so much easier. Everything you need can be included into a few main files — no more forgetting to include formfunctions.php into that statistics report page. You would not believe how good it feels the first time you realise you never have to include anything manually again.
Of course, with all that spare time you now have, you can get to work learning how to use the helpers that are now helpfully included into every page. No more manual validation of forms — just instantiate a new form validator object and tell it what you need. No more passing error messages in GET strings (and I've seen some terrible examples of this) — your form helper can flag errors on your fields when it reloads the page. Welcome to the land of milk and honey, where there are helper objects for all the fiddly things you once did by hand.
Less time coding — you're so efficient you just put yourself out of a job.
It doesn't take a genius to figure out that using a framework saves you stacks of time and effort. It really does enable you to do things you would only have dreamt of, back when every page had its own controller (or however you choose to do it — that's the sin I was guilty of).
As I mentioned, if you stick around I'll take you through how I wrote my own framework so you can write your own too. It's perfect for anyone too anal to bow down to someone else's way of naming objects :)
Have I missed anything? Are there disadvantages to frameworks you want to mention? Let me know in the comments.
Update: some further thoughts on the topic here.
Comments
And of course you can always write your own...
It might seem a bit overkill for a simple app like this, but the App itself is very small, and uses my central CakePHP library, so it allows me to run as many as I like, as well as other applications.
That aside, I don't really care what framework you use. I prefaced my talk with this specifically. I just encourage people to use *something*, or at least evaluate and justify properly why you're *not* using one (on new projects, etc).
My last workplace had their own in-house framework, which kind of counted as a framework. There was no doco, and the former developer(s) either didn't work there or were offsite. I was in the deep end, and I was over my head. At least was an off-the-shelf framework, I'd have a chance of surviving (well I *did* survive, to an extent - it could have been better though).
My major gripe is how willing PHP developers are to reinvent the wheel. It's just such a waste of time and resources. I assure you, in the majority of cases, someone else has already done it, and someone else has done it better. While we're wasting our time re-inventing the wheel, PHP will gain little traction in Australia - .NET and Java will continue to prevail.
Sad, but that's where we are these days. Oh well. One day things might change.
I also looked at Drupal and that require me to do too much tweaking before I can use properly.
Regards,
<a href="http://binh.name">Binh Nguyen</a>
For BIG projects I suggest Cake because it saves you a lot of time in the long run.
I dont like to code to much as well... ;/
You think CodeIgniter will be the right solution for me?
I was wondering if you have any metrics with regard to performance of straight php vs a framework, or at least any comments or thoughts? How important is it that straight php outperforms most frameworks?
It's hard to recommend anyone a framework, and even more difficult to get a straight answer from anyone, because they'll either recommend their framework with extreme bias because it's the only one they know. Heres my take from my last few weeks researching different frameworks:
CI (Code Ignitor) - Documentation is really crisp and easy to understand, not as powerful as others, but good for beginners to MVC and OOP.
Zend - Don't know too much about, but seems like it has a alot of Libraries built in, so it's going to be pretty powerful, but I also understand it has a steep learning curve.
CakePHP - Pretty large community, good resources, and I've found more tutorials on Cake than other frameworks. It's very MVC strict, whereas CI is very lenient (You don't have to use models in CI I believe). Encourages good code practice and agile development.
I ended up (after much debate) to go with CakePHP, seems most popular so there are a ton of people that will help you out with what you need. I'm extremely new to OOP (pretty much just grasped the basics last week) and my very first time experiencing MVC and so far I'm working well with it. CakePHP for me is really only the starting point to get me into an Object Oriented and MVC mindset, then in a few months after a couple applications, I'm switching into Ruby and (of course) rails. I have a few friends in it and they praise it, so that's my direction.
Hope that helps anyone, I've only been around back-end programming since September, so about 8 months of Procedural development, and I'm getting out. Cheers. Feel free to email me if you have any questions as well michael ]at[ thisiswilson ]dot[ com
Cheers!
Thanks.
Sorry for poor english.
"But PHP's ease of use is also its downfall. Because there are less restrictions on the structure of the code you write, it's much easier to write bad code. But there is a solution: use a framework. "
This is the fault of the developer. From what I've found, frameworks dumb every task down. For example, a bad or under experienced development might be using a framework and not know how to filter XSS. Many frameworks can globally filter XSS from $_POST and $_GET for you, and they never learn how to do this themselves, thus relying on that framework to do everything for them. It's almost like what PHP would be if it did all this for us to begin with.
Everything from pretty URLs, helpers, coding standards and MVC can be done on your own. The only good I see in a framework is that you can spend less time on a project if you know the ins and outs of the framework you are using, otherwise you will spend much more time looking up functions and methods at their documentation.
I'm not against frameworks, I am playing with Kohana and I like it a lot. But frameworks I don't believe should be used to make life simpler on you. You should use one when you know them very well and when you are confortable with your skills in PHP, otherwise you will remain a lame underknowledged developer who can't do simple tasks for themselves.
Also, I feel one of the biggest advantages of using frameworks is that most of them have ORMs built with them.
1) When something get stuck, you feel helpless and you need to search for their forums and limited resources.
2) It will NEVER work with all the platform and environment. If you use it on windows, you will have some issues, while on linux and other system it will have some other.
3) It's heavy, more compiling time. For huge traffic website, a BIG NO NO to frame work
4) You will always feel helpless when something gets stuck in between, some framework will not support latest version of PHP and will throw so many issues.
5) If you understand coding well, code in simple OOPS and secure coding rather than using any framework, it will give you much more control on your website and other issues like SQL, Speed and so many things.
6) Framewaork is like third party software, on which you can NOT rely upon.
So if your website is Important for you, and generating serious business, code in simple business and NEVER ever use any framework.
