PHP Application Management
Give a method to the madness of managing PHP applications.Published Dec 4, 2005 by lobo235
I have done a lot of work recently with a PHP application that was written by someone else. He quit his job and I took over his responsibilities. The PHP application includes hardly any comments in the code and apart from a few PHP classes that he wrote, nothing is reusable. The code is spaghetti; there are pieces of it everywhere and it takes forever to figure out what a certain function is supposed to do. As one of my first tasks, my employer has asked me to reorganize all of the code and make it easier to manage. In this article I will share some of my ideas for making PHP applications easier to manage.
The first thing that should be done is to create PHP classes out of anything that can be logically built as a class. PHP classes can be created from almost anything. Things like user accounts, database access, and authentication can be written as classes. Having these things in classes will make it easier to reuse the code that deals with them.
The second thing that I recommend is to organize your scripts in a logical manner. I typically will create a header script that has any PHP classes, common functions, or session starting functions in it. This header script gets included in almost every single page. If necessary, I also create a footer script that will perform any closing tasks that I need to do. I will then create separate pages for each section of the app and include the header and footer files at the top and bottom of the script. It's easier to manage a PHP application that is broken out into separate scripts that make sense. In the PHP application that I am trying to reorganize, the previous programmer wrote pretty much everything into one huge index.php file. Each page was then accessed by pulling up index.php?p=search or index.php?p=list, etc. This index.php file was thousands of lines long and trying to update it or find a certain area in the code was a nightmare. Breaking the code out into files like search.php and list.php makes it a lot easier to find things and it's sure easier than dealing with a thousand line PHP script.
The third thing that is important to make your PHP applications easy to manage is to write comments for functions, classes, or other areas of the code. If the app has some complicated scripting in certain areas it needs to be documented well. It will make it easier for you a year down the road when you have to work on that code again if you have commented it well. Also, if you are ever in a situation where there are multiple developers working on a project it becomes very necessary to comment your code well.
The fourth thing that I would mention would be to create a version system for your application. Save old versions of the code in case something goes wrong and you need to revert back to the old version until you can figure out what's going on. Create a change-log file to list changes made between versions.
The last thing is to create a document that explains how the application is organized. It should contain guidelines for adding features, writing comments, etc. In short, you want to place anything in this document that will help future developers know how things are being done.
I hope this information will be of use to you. It has helped organize the PHP application I have been referring to above. Please feel free to share your feedback with us about this article.
0 comments for this article.add this article to del.icio.us! Other great Web Development and Programming articles on Netlobo.com:
Resizing an Image Using PHP and the GD2 Library
Clean URLs using Apache's mod_rewrite