Ionrock Dot Org

by Eric Larson

My Weblog

Object Oriented Design

I have been working on a project lately and I have made the decision to try and use an object oriented design. What that means... I am not exactly sure. The first thing I started with was that instead of using functions to do the brunt of the work I would make an effort to create classes to take care of my processing needs. The next thing I wanted to avoid like the plague was one main page that took all the requests. I know this is a popular method that can be seen in the many cold fusion programming circles as well as with many php apps. I think it is a good thing to learn but it gets to be a headache in the end if it is anything but a simple program.

The other detail I wanted to get away from relied on having many different includes based on what needed to be done. This is a bit more difficult than my main page method, because it is just so darn easy. Each file becomes like an object in itself and does what it does in a clean and simple way. I have seen many many projects use includes as a means of organization and I have no real qualms with it. This makes it extremely hard to let it go.

Knowing what I don't want to do does not do much to give me a good idea of what I am actually going to do. After some testing and working out some ideas I hit irc to see what others had to say. I didn't really have a good question for anyone so it was a kind of shot in the dark strategy to see what people said about different aspects of php and object oriented design. One helpful chatter led me to look at php patterns, which is a method that looks for patterns in what code does as a means of creating efficient classes. While this was not much practical help, it made it clear that others have been where I was before and had come to positive conclusions that they can actually use. This was a good sign.

After reading a bit of a few articles I noticed a few mentions of a factory method which seemed to make sense. I found a small article that merely gave a brief explanation and it really made sense. A class is written that handles everything and calls other classes based on what is needed. It is really like a filter that directs information and actions to the appropriate authorities. I have not become an expert, but it did give me some ideas of how I could organize things and work with them that I thought I would try.

I made a page to update a database and create some forms and while it is not the easiest to understand, I think I made a bit of a step forward in my understanding of object oriented concepts. One thing someone said in irc is that it really just takes a lot of experience. I am seeing that this is very true. My first page (I decided to keep basic functions handled by one page, calendars by calendars.php, profile.php for profiles, etc...) went kind of crappy but the next page was 5 times as good and much better code. I am sure it is still not great but it is a start. Wish me luck.

Posted Thu Jul 8 12:08:57 2004 by Eric Larson
Created using Python, jQuery and Emacs