Modulating programming by including files

When I first started coding PHP, my source files looked like an aggregate of languages. I had inline CSS wrapped with PHP and a crude mixture of XHTML and HTML bouncing around the file like a bilingual child with ADHD. I took a little hiatus from coding for the past 8 months and returned to find an old website in shambles. Everything still functioned perfectly fine, aside from a score of permission problems, but the code was impossible to read. I began the immense task of “porting” the code over to a more readable, functional, and modular layout. I didn’t just code. I sat there and looked at it, examining every single class, function, and echo that produced the web app. I realized how easy it would have been to do this from the start, and I’m pretty disappointed with myself (considering I’m so concerned with having a solid set of standards to abide by).

This is what I’ve started doing, hopefully it will become habitual soon:

  • All classes are organized with a clear nomenclature and file location. For instance, all classes are as follows: /public_html/site_root/includes/classes/class.core.php It’s important to not use custom extensions (.inc, .whatever) as it could create huge security problems.
  • Without losing a significant amount of performance due to the heaviness of a framework (I still prefer to write all of my classes) I chose to avoid templating systems by this: I have your standard page.php file (right in the site root) with the necessary JavaScript and CSS (see below) file calls in the head. I write the layout using HTML5 and style it exclusively with a stylesheet. ZERO inline styles. The necessary programatic logic (that cannot be simply included) is written into the file (with heavy comments). The main logic of the page is included in a separate file, stored like the following: public_html/site_root/includes/page_name.descriptor.php
  • All CSS and JavaScript are in their own respective directories, such as /public_html/site_root/includes/css etc.
It’s also important to remember that you should be doing all of your session and database calls from a secure, single file. Don’t be opening new SQL connections in the middle of a random file, don’t store your database connection details in variables, etc. It’s amazing how easily beginners can forget this stuff.

Category: Internet Comment »


Leave a Reply



Back to top