Skip to content
zenmiu edited this page Jul 21, 2011 · 3 revisions

General requirements

Required:

Names of classes, functions and significant variables must always be self-descriptive, so that reader could instantly comprehend what they are used for.

Abbreviations

Required:

Naming format - Upper case [REQ.PHP.1.2.1].

Example:

class HTMLStatistic  // correct
class HtmlStatistic  // incorrect

Classes

Required:

  1. Naming format - Pascal Casing [REQ.PHP.1.3.1].
  2. The slash is used to define the path to the file with the definition of the class [REQ.PHP.1.3.3].

Example:

// Declaration in Product/Options.php

namespace Product;

class Options {
    ...
}

// Declaration in Viewer/XHTML/Helper.php
namespace Viewer\XHTML;

class Helper {
    ...
}

// Declaration in Name.php

class Name {
    ...
}   

Recommendations:

  1. Name a class only when you know what the class will do, how it will do that, and what for. If you do not know that, it is quite possible that you have not fully thought through the concept of the module.
  2. Do not be tempted to name a child class after its parent class. It is better that the class lives its own life, regardless of its parent.

Methods and functions

Required:

  1. Naming format - Camel Casing [REQ.PHP.1.4.1].
  2. Verb-form naming [REQ.PHP.1.4.2].

A method or function usually performs a certain action; therefore, its name must give a clear idea of the action it is made to perform. The difference between a method/function name and a property/variable name is that a method does something, while a property is or stores something.

Example:

function uniqueId()      // bad
function getUniqueId()   // good

function string()        // - bad
function isString()      // - good

function value()         // - bad
function setValue()      // - good

Recommended suffixes:

  1. Max - to indicate the maximum value.
  2. Count - to indicate the current value of a certain counter.
  3. Key - to indicate the key value. Example: RetryMax contains the maximum number of available attempts, and RetryCount - current attempt number.

Recommended prefixes:

  1. is - to indicate a question. Wherever you see is, you always know that this is a question.
  2. get - to indicate getting a value.
  3. set - to indicate setting a value.

Example: isHitRetryLimit() (Something like: Is this the last attempt?)

Example:

class Name
{
    function doIt() {};
    function handleError() {};
}

Variables, class variables, function arguments and array keys

Required:

  1. Naming format - Camel Casing [REQ.PHP.1.5.1].
  2. First word - noun [REQ.PHP.1.5.2].

Example:

$errorCode = false;
$productId = 0;

class Product {
    protected $errorCode;
    protecetd static $productId;
}

function getProduct($errorCode, $productId) { }

$myarr['fooBar'] = 'Hello';

Constants

Required:

  1. Naming format - Upper case [REQ.PHP.1.6.1].
  2. Word delimiter - underscore ('_').
  3. A constant declared outside the class begins with 'LC_' [REQ.PHP.1.6.3].

Example: define(‘LC_GLOBAL_CONSTANT’, ‘Hello world!’);

Internal constants: true, false, null

Required:

PHP’s internal constants – true, false and null – are typed in the lower case [REQ.PHP.1.7.2].

Files

Required:

  1. Naming format - Pascal Casing [REQ.PHP.1.8.1].
  2. Always use the .php extension [REQ.PHP.1.8.2].
  3. When building a path to a file, use the short constant LC_DS for delimiting items of the path.
Clone this wiki locally