Tuesday, January 5, 2010

The New Year! ...and Correct PHP Code Design

Its been a while since my last post, but I've made it a sort of new years resolution of mine to start posting more regularly here. Now my focus will not be solely on AI stuff, rather I will cover any tech topic that I am learning about or using.

...Catching Up
So over the past few months, I have put a lot of time into developing my website, www.spareourgreen.com. If you actually want to hear about what the website does, go ahead and visit it, here I will just be covering the technology used. Never having written any websites other then straight HTML and Javascript, I was excited to learn PHP. After reading a book about PHP basics, I dove right in and started coding the first 5 or 6 pages. I finally got all of them working correctly, but then realized a problem I was having, every time I added a new menu item, I would have to add the new link to each page individually. ThenI came to the realization that I should dynamically load all of my web pages from the one centralized source, and keep individual page content in its own file. This way, updating the menu or the footer makes the change across all the pages.

...Another Challenge
Now, after nearly 4 months of working on this code, and having different pages load different content into their menu's and such, my code is once again a horrific mess! Now when a page breaks after adding a small new feature, I have to pick through hundreds of lines of unstructured code to try and find my problem. Usually I know where the problem lies, but even then it is still a pain to find. After recently adding a new section to my website that has vastly increased the complexity of my code, I have decided to wipe out all my old code (not really, never delete your old code, just .7z it and save it on your HDD), and re-write the website using well-structured, scalable, object-oriented programming in PHP.

With object orientation, making a broad change across all of my pages is as simple as adding a new function or method to an existing class, or adding a new class to my main .class.php file. Then, I can choose which pages this needs to be implemented on, and make 1 new function call on those pages. The way I am using the OOP actually could be implemented the same way I had done it previously by using just 1 php file and adding new methods, but like I saw, that can get pretty messy. Working with objects is much easier to keep tidy.

...Progress
Right now, I have just begun to undertake this new initiative. I have written a User class to store data about the person visiting the site, as well as a Page class which is what will be my initial call on every php page. So here is what a sample site could look like:

<php require_once(../classfile.class.php); $file = new Page(); ?>
........HTML or PHP page specific content goes here............
<?php $file->close() ?>

As I continue to work on this code, I will keep updating on my progress and try to provide information on any problems that I run into so that you may learn from my mistakes.

No comments:

Post a Comment