PHP
programming summary
PHP is a
server scripting language, and a powerful tool for making dynamic and
interactive Web pages.
Naming
Conventions
Classes
Class names
may only contain alphanumeric characters. Numbers are permitted in class names
but are discouraged in most cases. Underscores are only permitted in place of
the path separator; the filename If a class name is comprised of more than one
word, the first letter of each new word must be capitalized.
Abstract Classes
bstract
classes follow the same conventions as classes, with one
additional rule: abstract class names must end in the term, “Abstract”, and
that term must not be preceded by an underscore
eg:-
abstract class Abstract
{
// ...
}
Interfaces
interfaces
follow the same conventions as classes, with one
additional rule: interface names may optionally end in the term, “Interface”,
but that term must not be preceded by an underscore
Filename
For all
other files, only alphanumeric characters, underscores, and the dash character
(“-”) are permitted. Spaces are strictly prohibited. File names must map to
class names as described above
Functions and Methods
Function names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in function names but are discouraged in most cases.Function names must always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word must be capitalized. This is commonly called “camelCase” formatting.
These are examples of acceptable
names for functions:-
1.filterInput()
2.getElementById()
3.widgetFactory()
Variables
Variable names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in variable names but are discouraged in most cases.For instance variables that are declared with the “private” or “protected” modifier, the first character of the variable name must be a single underscore. This is the only acceptable application of an underscore in a variable name. Member variables declared “public” should never start with an underscore. variable names must always start with a lowercase letter and follow the “camelCaps” capitalization convention.
Coding Style
<?php
?>
Strings
String Literals
When a string is literal (contains no variable substitutions), the apostrophe or “single quote” should always be used to demarcate the string:String Literals Containing Apostrophes
eg:-
$sql = "SELECT `id`, `name` from `people` "
. "WHERE `name`='Fred' OR `name`='Susan'"
String Concatenation
Strings must be concatenated
using the ”.” operator. A space must always be added before and after the ”.” operator
to improve readability
eg:-
$sql = "SELECT `id`, `name` FROM `people` "
. "WHERE `name` = 'Susan' "
. "ORDER BY `name` ASC ";
Arrays
An indexed array may start with any non-negative number, however all base indices besides 0 are discouraged.When declaring indexed arrays with the Array function, a trailing space must be added after each comma delimiter to improve readability:
eg:- $sampleArray = array(1, 2, 3, 'Zend', 'Studio');Alternately, the initial array item may begin on the following line. If so, it should be padded at one indentation level greater than the line containing the array declaration, and all successive lines should have the same indentation; the closing paren should be on a line by itself at the same indentation level as the line containing the array declaration:
Associative Arrays
hen declaring associative arrays
with the Array construct, breaking the statement into
multiple lines is encouraged. In this case, each successive line must be padded
with white space such that both the keys and the values are aligned:
$sampleArray = array('firstKey' => 'firstValue',
'secondKey' => 'secondValue');
Classes
The brace should always be written on the line underneath the class name.Every class must have a documentation block that conforms to the PHPDocumentor standard.
All code in a class must be indented with four spaces.
Only one class is permitted in each PHP file.
Placing additional code in class files is permitted but discouraged. In such files, two blank lines must separate the class from any additional PHP code in the class file.
/**
* Documentation Block Here
*/
class SampleClass
{
// all contents of class
// must be indented four spaces
}
class SampleClass extends FooAbstract implements BarInterface
{
}
Class Member Variables
Member variables must be named according to
Zend Framework’s variable naming conventions.Any variables declared in a class must be listed at the top of the class, above the declaration of any methods.
The var construct is not permitted. Member variables always declare their visibility by using one of the private, protected, or public modifiers. Giving access to member variables directly by declaring them as public is permitted but discouraged in favor of accessor methods (set & get).
functions and Methods
Function and Method Declaration
Methods inside classes must always declare their visibility by using one of the private, protected, or public modifiers.
As with classes, the brace should always be written on the line underneath the function name. Space between the function name and the opening parenthesis for the arguments is not permitted.
Functions in the global scope are strongly discouraged.
eg:-
/**
* Documentation Block Here
*/
class Foo
{
/**
* Documentation Block Here
*/
public function bar()
{
// all contents of function
// must be indented four spaces
}
}
Control Statements
Control statements based on the if and elseif constructs must have a single space before the opening parenthesis of the conditional and a single space after the closing parenthesis.Within the conditional statements between the parentheses, operators must be separated by spaces for readability. Inner parentheses are encouraged to improve logical grouping for larger conditional expressions.
The opening brace is written on the same line as the conditional statement. The closing brace is always written on its own line. Any content within the braces must be indented using four spaces.
if ($a != 2) {
$a = 2;
}
Switch
Control statements written with the “switch” statement must have a single space before the opening parenthesis of the conditional statement and after the closing parenthesis.All content within the “switch” statement must be indented using four spaces. Content under each “case” statement must be indented
switch ($numPeople)
{
case 1:
break;
case 2:
break;
default:
break;
}