Perhaps you were looking for a:
Recent project
Saturday, March 22, 2008, 2:49 PM
I’ve been learning a lot at work lately, taking it all in as I have to implement it. One of the senior developers just left, but before he did we’ve been having a lot of meetings about programming concepts, standards, and best practices.
To seriously take my mind of things at home, I started a new PHP project for my personal gratification, learning, and possibly even distribution as an open-source solution. It started out with the realization that making code custom for every site you do sucks. Yes, you have to customize everything at some point, but I decided that I could make a basic framework as a starting point. This means you have everything right there functionally at your fingertips and you can fill in the gaps as needed.
I decided I needed to take an object-oriented approach right off the bat. Past projects, while functioning perfectly, have been a pain in the gut to maintain just because I did not use OO techniques meaning you get a slew of procedural muck. Not being a OOP genius yet, I thought about what I needed first: A DAO (Data Access Object). Since all the components of this framework are going to be database driven, it only makes sense.
My DAO is very simple. Commonly you will have a DAO for each type of object you want to create, but for now it is a general DAO (so it can be extended by other objects, but isn’t yet). It’s properties are simple and few: Server (the name or IP of the server you want to connect to), DB (the database name), User (the db username), Pass (the db password), Con (the actual connection resulting from mysql_connect() or new mysqli()), Query (the SQL string you’ll pass into the db), and Result (the MySQL result set you get back after execution with mysql_query() or query()). At this point the methods are very simple and few also: Accessors for each property, a constructor that sets the connection, executeQuery() (pretty obvious functionality), returnNumRows() which gives you the number of rows gathered from the query, returnResultAsArray() which gives you an associative array based on the MySQL result, and close() which kills the connection and returns NULL to (sort of) unset the DAO object.
It’s easy to see how this effectively wraps database functionality into a nice little object. It gives you all the functionality you’ll need for any database call, although if you do start creating different objects with complex database structures it would be suggested to create individual DAOs for them and extend this base DAO. If I want to change databases or the way I connect (for example using mysqli() instead of classic mysql_connect() + mysql_select_db()) I can do that in one place instead of going through every class and every code page and change every instance of those functions. That would suck - I’ve done it before.
Anyway, after having built that cute DAO, I started making my first component, a navigation. This one’s pretty cool - it’s not a huge component but it gives me enough of a feel for how much actually goes into something you want to make extensible, customizable, and distributable. Holy crap, man! Lol. I’d like to go into detail about it, but I’ll save that for the next post. But basically its usage is simple: You instantiate a Navigation object, pass in its name, base URL (either relative or absolute), and an orientation (so the nav goes either horizontal or vertical). After that you simply call the render method and pass in the mode - either CSS which builds the display code off of colors and whatnot that you set in a config file, or the default of none which leaves it completely unstyled and allows you to implement your own stylesheet. It’s pretty cool. Like I said, I’ll get into the code in the next post - it’s really interesting (to me anyhow). :)
All in all it’s a really fun project and allows me to stay up a little too late (3AM last night). Woo!