RSS

Object-oriented PHP and Codeigniter

This entry was posted on Aug 08 2008

My project of the moment is my restaurant review concept, Yum Houston.  I’ve been working for about three weeks and I’m now in the early development.  I’m starting off by building a large codebase of classes that I’ll need.  My framework of choice for going from idea-to-product fast is (as always) Codeigniter.  Armed with my trusty-duty sidekicks Dreamweaver CS3 and Filezilla, development has been quick and easy =).  I also recently moved off my terrible dedicated server onto a new Grid account with Mediatemple (mt).  Everything is fast, easy to use, and I’m extremely motivated.

I’m taking my time on this project, trying to make sure I code things right the first time.  I’m trying to use as much object-oriented (OO) principles as seem appropriate as I go (you know, make the Rice CS profs proud).  I’ve developed using Codeigniter (CI)  several times before, but exactly how to apply my in-class OO knowledge in the Codeigniter framework escapes me at times:  I’m usually extremely uncertain as to where I should create my own classes that actually DO STUFF.  There are these controller and model classes floating around, but where do I put my class “Restaurant” that knows its location and its food type and knows how to display itself?

I stumbled across a great introduction to writing applications using CI on Jim O’Halloran’s Weblog – I ended up deciding to create a “single record” class Restaurant that was a model.  I gave it the appropriate fields (location, food type, name) and wrote a load($id) method.  I’ve honestly never written OO code that accessed a database before, so the concept is new to me.  Generally, you load the data from the db into your object:  my load($id) function takes a restaurant id, looks up its name, location, and food types, and sets its fields to the appropriate values.  Getters and setters let me modify the object.  And when I’m ready to save it back in the database, my save() function does just that.  This is apparantly really common, I remember seeing it before.  Up til now, I just havn’t realised where I could use it.

This brings me to the second point of this post.  I’ve been happily coding for about a week now on my new (mt) account with CI.  While I’m developing all sorts of errors show up, usually because I’m careless, or I don’t quite remember PHP syntax or CI convention.  While I was trying to code all “OOey” I tried modifying the visibility of some of my class fields.  PHP kept throwing my “privates” back in my face, though.  public, protected, it didn’t matter.  I also tried privatizing one of my controller functions.  When private function function_name() didn’t work, I even tried swapping the function and private, in a last ditch attempt.

It didn’t take long before I realised that the keyword var seemed to always solve my field problems.  And the var keyword looks so very old in my nubile PHP-coder eyes.  That’s when I decided to throw up a phpinfo() in my script.

Sure enough,

PHP Version 4.4.8

I’m terrified that it’s taken me this long to figure out that I’m not running “all new, with great OO!” PHP5.  All that I even noticed was that __construct() and private never worked.  What were some other clues that could have tipped me off?

Well, anyway, I’ll be sure to look into how to switch to PHP5 tomorrow — It’s just a setting in the (mt) control panel, I’m sure.  And then hopefully I’ll be off __construct()ing and privating in no time.


One Response to “Object-oriented PHP and Codeigniter”

  1. Alright, you wanted a comment, so I got one a better, a few questions.

    1. How much wood could a wood could a wood chuck chuck, if a wood chuck could chuck wood?

    2. Have you found any major drawbacks with CodeIgniter?


Post a Comment