So here we go, this is a bit of a rant section regarding how I see other people program PHP. These are my opinions, so if you disagree please comment; we’ll chat it up. I may learn something.
Site functions/classes
Let’s use a calendar application for the sake of this argument. There are most likely functions that are used with a calendar that you’re using, fair enough. But let’s say there’s only one asset on the site that uses the functions for this calendar. They should be contained in the asset that uses them.
Now if there’s multiple assets that require these functions then break them off into an included file such as: calendar_functions.inc.php. Then include that on any page that requires such functions. This organizes the site so that if another developer (or yourself for that matter) needs to modify or examine the calendar functions then it is in a clearly labeled include file.
What I’m getting at here folks is don’t use a sitefunctions.php file in order to keep EVERY function/class for EVERY asset of the site, this results in a bloated 3000 line file that rapes the eyes and mind with any edit you need to do. I’ve seen this method one too many times; and every time I end up breaking it up into multiple include files or putting the functions on the local asset that uses them if they’re only used once.
Commenting any functions or classes is also very important for further development and editing. This means that you pre-empt any function with a comment in order to keep track of what every function/class does. This doesn’t apply to PHP only; this applies to all programming.
If it is a local asset for a singular file then put the functions/classes at the beginning of the file so it is clear to any programmer that you intend to use these functions in this asset.
Now lumping like functions together in a file is perfectly acceptable. I see this used mostly when it comes to image handling and session purposes. Lumping like functions makes sense when it comes to organized web development.
Rules of Three
This piggy backs onto the site functions/classes section. Basically the rule of three goes like this:
If do/use something three times then function/class it out.
Same goes for included functions, instead of bloating three or more like files with the same functions, then break it out into an included file in order to organize code. This makes the functions easier to globally edit as well as cuts down on lines of code.
PHP as a HTML Parser
Yes, arguably PHP is an HTML parsing language. But proper web development does not require you to use PHP to parse an entire site. This makes it damn near impossible to edit the HTML, CSS, or Javascript for individual files. Some people think that a file ending in .php should be only PHP. This is far from the truth, PHP should be used as a tool in order to build HTML; not build an entire site.
<? And <?php
Ok – small rant section. If you build a site using “<?” as an opening PHP tag – good. If you build a site using “<?php” as an opening tag, also good. For the love of God stop using them both in the same file. Standardize please.
Note: Using “<?=” with “<?” is perfectly fine. Actually it’s very shiny.
Comments
I can’t stress this enough. Just comment, make them useful. Even if things look as if they should be perfectly reasonable and understandable to figure out by the source code remember you just programmed it so of course you’re going to understand it. Two months down the line, you may not… and another developer is sure as hell not going to be able to understand it.
Now I don’t want to see a comment before EVERY line of PHP… but if you have a loop that parses news items, then do something like this “//Loop to parse news items”. Simple.
Variable/Function Names
For the love of God make them make sense. Query strings should have “query” in them somewhere. results should have “result” in them somewhere. Rows should, well, have “row” in them somewhere.
Count variables should usually be i, j, or k. Standard programming procedures.
If a variable deals with something, then have the name reflect that. I’m so tired of seeing random variable names like “$sleepy_lama” and what not. This tells me nothing, and the humor of it is drowned out by my utter desire to slap you upside the head.
Now in the defense of humor… I’ve seen it used well before. There was a function I saw once that dealt with CUPS; and one of the variables in the file was “$two_girls_one_cups”. Absolute hilarity and it still tells me what the variable dealt with.
Easter Eggs
Just no.
Conclusion:
Programming things in an organized fashion leads to me not hurting you. Programmers aren’t usually the most of stable of individuals and if they have to hack through your source for over an hour… you’re physically at risk. Programmers are creatures that prefer working in the dark, loading their bodies with caffeine and nicotine… and usually have seen enough slasher flicks to know exactly how to dispose of a body. This alone should force you to code in an organized fashion.
So go fourth and program organized code.