Coding standard - shepko/PHP-Manifest GitHub Wiki

This is initial document and I hope it will be finished soon. Community support is encouraged!

This coding standard is mostly ispired by Zend coding standard.

Coding standards are important in any development project, but they are particularly important when many developers are working on the same project. Coding standards help ensure that the code is high quality, has fewer bugs, and can be easily maintained. Code conventions are important to programmers for a number of reasons:

  • 40%-80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original author.
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
  • If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.

Overview

This document proposes guidelines for code formatting and documentation to pragmatic software engineers. I found out that established standard is more important than what that standard actually suggests at the most detailed level of design.

File formatting

General

For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response.

Indentation

Indentation should consist of 4 spaces. Tabs are not allowed.

Maximum Line Length

The target line length is 80 characters. That is to say, developers should strive keep each line of their code under 80 characters where possible and practical. However, longer lines are acceptable in some circumstances. The maximum length of any line of PHP code is 120 characters.

Line Termination

Line termination follows the Unix text file convention. Lines must end with a single linefeed (LF) character. Linefeed characters are represented as ordinal 10, or hexadecimal 0x0A. Do not use carriage returns (CR) as is the convention in Apple OS's (0x0D) or the carriage return - linefeed combination (CRLF) as is standard for the Windows OS (0x0D, 0x0A).

Naming conventions

Filenames

Classes

Abstract classes

Traits

Interfaces

Exceptions

Function and Methods

Variables

Constants

Namespaces

Coding style

Code Demarcation

Including Code

Semicolons

Global Variables

Expressions

Operators

String

String Literals

String Literals Containing Apostrophes

Variable Substitution

String Concatenation

Arrays

Numerically Indexed Arrays

Associative Arrays

Classes

Class Declaration

Class Member Variables

Functions and Methods

Function and Method Declaration

Function and Method Usage

Control Statements

If/Else/Elseif

Switch

Type hinting

Chaining

SQL inside PHP

Inline Documentation

Documentation Format

Files

Classes

Functions

Best practices

Bad practices