Naming Conventions
In this lesson we will look at the naming conventions used in PHP.
Variables
Variables should contain only lowercase letters, use underscore separators and be reasonably named to indicate their purpose and contents. Very short, non-word variables should only be used as iterators in for()
loops.
Example: Correct
//Rightfor ($j = 0; $j < 10; $j++)$str$buffer$group_id$last_city$first_name = “John”;$last_name = “Doe”;
Example: Incorrect
//Wrong$n1 = “John”;$n2 = “Doe”;$my_name = “John Doe”;$my_var = “John Doe”;$j = 'foo'; // single letter variables should only be used in for() loops$Str // contains uppercase letters$bufferedText // uses CamelCasing, and could be shortened without losing semantic meaning$groupid // multiple words, needs underscore separator$name_of_last_city_used // too long
So can you guess what’s wrong with $n1
, $n2
, $my_name
and $my_var
? One word – “Ambiguity”. Ambiguous names are the biggest reason for unreadable and ugly code. So what is “Ambiguity”? It’s simply naming a variable in a way so that you can’t guess what exactly it stores. Consider for example following snippet:
$i = “Apple iPhone 6S (32GB, Rose Gold)”;$p = “749.00”;$t = “749.00”;
Can you guess in the above code, what is $t
? No? Okay, let’s try the following code:
$item = “Apple iPhone 6S (32GB, Rose Gold)”;$price = “749.00”;$total = “749.00”;
Can you guess now? Exactly! This is how you differentiate ambiguous variables from absolute variables. Obviously the latter approach is better because it is definitive.
Constants
Constants follow the same guidelines as do variables, except constants, should always be fully uppercase. Always use CodeIgniter constants when appropriate, i.e. SLASH, LD, RD, PATH_CACHE, etc.
Example : Incorrect
myConstant // missing underscore separator and not fully uppercaseN // no single-letter constantsS_C_VER // not descriptive$str = str_replace('{foo}', 'bar', $str); // should use LD and RD constants
Example : Correct
MY_CONSTANTNEWLINESUPER_CLASS_VERSION$str = str_replace(LD.'foo'.RD, 'bar', $str);
TRUE, FALSE, and NULL
TRUE, FALSE, and NULL keywords should always be fully uppercase.
Example : Incorrect
if ($foo == true)$bar = false;function foo($bar = null)
Example : Correct
if ($foo == TRUE)$bar = FALSE;function foo($bar = NULL)
Functions
There are two functions majorly used:
-
lowercase with underscore separators
-
camelcase
Example
Consider the following two examples:
function get_name(){// Do something}function getName(){// Do something}
So which one is best – none, both are equally good!
Always keep in mind that a function should always start with a “Verb” which defines what that function is trying to do in conjunction with the name of the “Entity” being affected by this function.
Example: Incorrect
databaseConnection() // WrongnamesList() // Wrong
Example: Correct
getName() // RightlistNames() // RightsendEmail() // RightdeleteUserById($id) // RightconnectToDatabase() // RightprepareOutput() // Right
Class Methods
Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased.
Example: Incorrect
class superclassclass SuperClass
Example: Correct
class Super_class
Class methods should be entirely lowercased and named to clearly indicate their function, preferably including a verb. Try to avoid overly long and verbose names. Multiple words should be separated with an underscore.