PHP Naming Conventions

Naming variables, functions, classes and files

Variables

All variables must be lowercase with underscore for “space”

$a = 'something'; $another_var = 49; $yet_another_long_var = 48;

_____________________________________________________________________________________________________

Arrays and loops

Arrays follow the same rule for variables, except data from database should be named $data, and in loops should always have $key => $row pairs

foreach ($data as $key => $row) { echo $row['something']; }

or if you don’t need keys

foreach ($data as $row) { echo $row['something']; }

___________________________________________________________________________________________

Functions

Functions must follow camelCaseConvention, which means no spaces and no underscores, lowercase with first letter of each word – uppercase (except the first word)

function fetch () { ... } function anotherFunction () { ... } function yetAnotherLongFunction () { ... }

All names should be descriptiove and explain what function does or returns.

__________________________________________________________________________________________

Classess

Class names are lowercase with each first letter uppercase and underscore (“_”) for space

class Database { ... } class Mysql_Database { ... } class My_Other_Class_Name { ... }

At the end of each class add comment: // end of classclass User extends Object { } // end of class

___________________________________________________________________________________________

Files

Classes should be saved as {$class_name}.php

require_once "Database.php"; require_once "My_Other_Class_Name.php";

__________________________________________________________________________________________

Common variables

All counters must be named as $i. If you have more than one counter, use the next letter: $j$k$l

for ($i = 0; $i < 100; $i++)

All temporary variables must be called $temp or $tmp;

for ($i = 0; $i < 100; $i++) { $temp = $a[$i]; $a[$i] = $b[$i]; $b[$i] = $temp; }

_________________________________________________________________________

Function names

Try to use as descriptive names as possible, but avoid to many words or to long names.

If you have class User and function to validate if this use is valid or not, instead of this

if (User::isValidUser ())

Or even worse

if (User::checkIfThisUserIsValidAndActivated ())

Use this

if (User::isValid ())

Becauses there’s no need to reapeat word “User” since it’s already in the class name

___________________________________________________________________________________________

Function prefix

All functions that return some $variable should be prefixed with “get“.

$username = getUsername (); $data = $object->getLastItem (); $user = Class_Name::getUserById ($id);

All functions that set object variable should be prefixed with “set

setUsername ($username); $object->setLastItem ($item); Class_Name::setUserId ($id);

__________________________________________________________________________

Constants

Global Constants

All global constants (constants that are used everywhere in application, not just single class) must be uppercase

define ('USER_TIMEOUT', '3000'); define ('LIMIT_POSTS', 15);

Class constants

Constants that are used in a single class should be defined as class constants, not global constants. Uppercase but without prefix, and must be defined first before any other class variable

class User { const LIMIT = 2; const ANOTHER = 'test'; } echo User::LIMIT;

__________________________________________________________________________

Test Values

Dummy variables

All test variables should follow those rules

$url = "www.example.com"; $string = "dummy"; // dummy strings $temp = 'some temp value'; // temporary values

ayzeetech

I'm Ayaz Ahmed Mast, I am a web developer and Instructor at Al Fateem Academy. My current weapons of choice are PHP CodeIgniter, jQuery, AJAX and Laravel. Being a Developer, I help clients bring their ideas into reality. Contact me today and I would love to hear about your business.

Leave a Reply

Your email address will not be published. Required fields are marked *