March 12, 2009 – 11:08 am
I think i had the basic idea about object oriented programming back in the early 80s – when i was an assembler programmer. Back then, i tried to make my code as modular and reusable as possible. But, in those days, at least, writing in assembly language didn’t give you much scope for optimising code in that way – the best i could do was use lots of subroutines.
Since i started working with PHP, a year or so ago, i’ve been using OO concepts a bit. I’ve written a couple of classes to do things that seemed to require it, and i always use the object oriented style for functions if it’s available. However, that’s as far as it went. I understood the concepts of OOP, but i didn’t really understand its application.
A few days ago, however, i started playing around with CodeIgniter and i was impressed. So i decided to rewrite a project i’m currently working on using the CodeIgniter framework. And now, as a result of using it, i think i really do finally get object oriented programming!
In the words of their web site, “CodeIgniter’s an Open Source Web Application Framework that helps you write kick-ass PHP programs”. It uses the “Model – View – Controller” approach, which separates logic from presentation and, if used properly, forces you to write code in an object oriented and modular way.
Controllers are functions that run first when a URL is requested. They then load one or more Views – which is where all the HTML is. Models are where your database functions go – and they can be called from either a controller or a view. Keeping these aspects of code separate helps considerably with reusability and it also helps keep individual sections of code simple. For me, at least, this approach seems to noticeably reduce the number of errors in my code.
CodeIgniter comes with a lot of useful built-in functionality – although the framework’s filesystem footprint is surprisingly small (only about 3MB). The included libraries help with things like accessing a database, sending email, validating form data, maintaining sessions, manipulating images, working with XML-RPC data, etc – most of the functions that you need to use all the time when developing web applications. The database abstraction layer not only simplifies the code, but should help with portability.
One of the best things about it, from my point of view, is that there’s no fancy template stuff involved – you just use PHP. Rewriting the project that i’m currently working on, to fit into the CodeIgniter framework, has proved to be relatively easy. Mostly, i can just cut and paste from the old php files into new ones – putting the relevant bits into either controllers, views, or models. I’ve had surprisingly few problems doing that – and i’m quite sure that’s because breaking the code down into smaller, more modular chunks has helped eliminate mistakes that would creep in otherwise.
I’m sure the work i’ve had to do rewriting this project will pay dividends in the time that i’ll save on the parts i’ve still got to do – and also in the added ease of maintenance and modification. And, as a bonus, i’m learning my way around a framework that i’m sure i’m going to be using a lot in future.