-
Notifications
You must be signed in to change notification settings - Fork 0
Naming
Required:
Names of classes, functions and significant variables must always be self-descriptive, so that reader could instantly comprehend what they are used for.
Required:
Naming format - Upper case [REQ.PHP.1.2.1].
Example:
class HTMLStatistic // correct
class HtmlStatistic // incorrect
Required:
- Naming format - Pascal Casing [REQ.PHP.1.3.1].
- 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:
- 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.
- 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.
Required:
- Naming format - Camel Casing [REQ.PHP.1.4.1].
- 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:
- Max - to indicate the maximum value.
- Count - to indicate the current value of a certain counter.
- Key - to indicate the key value. Example: RetryMax contains the maximum number of available attempts, and RetryCount - current attempt number.
Recommended prefixes:
- is - to indicate a question. Wherever you see is, you always know that this is a question.
- get - to indicate getting a value.
- set - to indicate setting a value.
Example: isHitRetryLimit()
(Something like: Is this the last attempt?)
Example:
class Name
{
function doIt() {};
function handleError() {};
}
Required:
- Naming format - Camel Casing [REQ.PHP.1.5.1].
- 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';
Required:
- Naming format - Upper case [REQ.PHP.1.6.1].
- Word delimiter - underscore ('_').
- A constant declared outside the class begins with 'LC_' [REQ.PHP.1.6.3].
Example: define(‘LC_GLOBAL_CONSTANT’, ‘Hello world!’);
Required:
PHP’s internal constants – true, false and null – are typed in the lower case [REQ.PHP.1.7.2].
Required:
- Naming format - Pascal Casing [REQ.PHP.1.8.1].
- Always use the .php extension [REQ.PHP.1.8.2].
- When building a path to a file, use the short constant LC_DS for delimiting items of the path.