PHP coding structure to Avoid Confusion.

In the web design world, there is a huge difference between how you picture something working and how it actually ends up working. Shortcuts often get taken to get the task done which ends up leading to confusing code; or confusing code can lead to shortcuts being taken, which only makes the problem worse. This ends up with a user interface (UI) that was not planned, and an unmanagable website for someone else to manage who is unfamiliar with the code. Problems start to show up accross the board due to the unorganized coding

What can a PHP developer due to prevent this problem? Is there a ‘one fits all’ structure? Probably not. Overtime I notice my PHP code evolving into similar structures on multiple websites. Most of my websites have multiple ‘class’ files with all my useful functions for that particular section. For example, if I have a booklisting system, I would have a folder named ‘booklisting’ with files named booklisting.php, booksearch.php, mybooks.php. These files contain only page flow for their respective functions. So where does all the actual code happen? Inside of my ‘booklisting’ folder there is a subfolder named ‘class.’ Withing this folder are files named booklisting_class.php, booksearch_class.php, and mybooks_class.php. These files contain all the functions I need for each of my flow control pages. There is also a config.php file inside the class file; this file is used for any functions that will be used across all booklisting pages. If there is a function I want to use across my entire site, I have a functions.php file in my main directory in a folder named ‘includes.’ With this hierarchy of functions, I can precide which file contains the function I am looking for; along with comments and formatting I can find it with ease. This code layout also forms a layer of flexibility to your site, I can place a booksearch box on any page I want simply by including my ‘booklisting/class/config.php’ file and calling the function responsible for showing the search box. This coding format, also adds a layer of continuity to all pages on your website, by using the same code, the search box will look exact on all pages. This also allows for you, the coder, to justify spending more time working on it knowing it will be used time and time again.

This is my method. Find a method that works for you, and run with it. It is bound to change over time regardless; happy rewriting.

PHP guru, Jpauls/Jeff Schollaart

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

I have found a really cool method….
i use joomla

:D

Leave a comment

(required)

(required)