2012040909550961

I. Preface of Standards

Standardization is not a special personal style. It allows programmers to understand any code and grasp the state of the program; enables newcomers to quickly adapt to the environment; prevents people new to PHP from repeatedly making the same mistakes; and in a consistent development environment, reduces the chance of mistakes. This standard largely follows the Java technology system in most applications, because the Java technology system has become the industry standard for most computer application layers due to its many successful cases. In addition, it facilitates the company's future transition to the Java technology system.

II. Naming Definition Section

Local variable naming

Use English nouns and verbs. Use uppercase letters as word separators, with all other letters in lowercase. The first letter of each word should be lowercase, and underscores should not be used. Example:

$repeatCount = '';
$delUserSql  = '';

Global constant naming

Use English nouns and verbs. All letters should be uppercase, with underscores separating each word. Example:

define( 'WEBSITE_NAME', '名称' );
define( 'WEBSITE_URL',  '地址' )

Array variable naming

Use English nouns and verbs. Use uppercase letters as word separators, with all other letters in lowercase. The first letter of each word should be lowercase, and underscores should not be used. Use the string "Array" as the suffix. Example:

$scopeArray  = array();
$bookIdArray = array();

Static variable naming

Use English nouns and verbs. Use uppercase letters as word separators, with all other letters in lowercase. The first letter of each word should be lowercase, and underscores should not be used. Use the string "Static" as the suffix. Example:

function getDirectoryFile()
{
    static $fileArrayStatic = '';
    static $fileNumStatic   = '';
    ...
}

Object variable naming

Use the class name as the variable prefix. All letters should be uppercase, with the string "_OBJECT" as the suffix. Example:

$USERACCOUNT_OBJECT   = new UserAccount();
$PAINTINGORDER_OBJECT = new PaintingOrder();

Class naming

Use English nouns. Use uppercase letters as word separators, with all other letters in lowercase. The first letter of the noun should be uppercase, and underscores should not be used. Example:

class UserAccount
{
    ...
}
class PaintingOrder
{
    ...
}

Method naming

Use English nouns and verbs. Use uppercase letters as word separators, with all other letters in lowercase. The first letter of each word should be lowercase, and underscores should not be used. Example:

class UserAccount
{
    function isAccountOk()
    {
        ...
    }
    function addAccount()
    {
        ...
    }
}

Parameter naming in methods

Use English nouns and verbs. Use uppercase letters as word separators, with all other letters in lowercase. The first letter of each word should be lowercase, and underscores should not be used. Example:

class UserAccount
{
    function isAccountOk( $accountName )
    {
        $this->accountName = $accountName;
        ...
    }
    function addAccount( $inputDataArray )
    {
        $this->inputArray = $inputDataArray;
        ...
    }
    var $accuntName = '';
    var $inputArray = '';
}

Class property naming

Use English nouns and verbs. Use uppercase letters as word separators, with all other letters in lowercase. The first letter of the word.

Use uppercase, and do not use underscores. For class properties that are object variables, use the string "Object" as the suffix. Example:

class UserAccount
{
    function IsAccountOk()
    {
        ...
    }
    function AddAccount()
    {
        ...
    }
    var $tableName      = '';
    var $databaseObject = '';
}

III. Syntax Writing Section

Braces {} rules

Place the braces on the same column below the keyword. Example:

if ( $condition )
{
    ...
}

Do not use this style:

if ( $condition ) 
{
    ...
}

Code indentation rules

Use tab indentation (TAB key) or four spaces. If the indentation level is greater than four, please redesign the algorithm for that business logic.

Parentheses () rules

Do not place parentheses closely against keywords, method names, or method parameters. Separate them with a space. Example:

if ( $condition )
{
    ...
}
function addAccount( $inputDataArray )
{
    ...
}

Because parentheses closely attached to keywords etc. can easily be seen as one unit, do not use the following style. Example:

if ($condition) 
{
    ...
}
function addAccount($inputDataArray)
{
    ...
}

if .. else ... Rules

It is usually best to have an else block to handle unhandled or unknown other cases. Even if the conditional processing statement has only one statement, braces {} must be used. Example:

if ( $condition1 )
{
    ...
}
else if ( $condition2 )
{
    ...
}
else
{
    ...
}

Avoid the following usage as much as possible. Example:

if ( $condition1 )
    ...
else
    ...

switch rules

Each case block must end with break, and default should always exist to handle unknown cases. Example:

switch( $condition )
{
    case $value1:
        ...
        break;
    case $value2:
        ...
        break;
    default:
        ...
        break;
}

Declaration positioning rules

Declaration code blocks need to be aligned, and variables must be initialized when first used. Example:

var $tableName      = '';
var $databaseObject = '';

Do not use the following style. Example:

var $tableName;
var $accuntName = '';
var $databaseObject = '';

IV. Other Notes Section

All class methods must have return values. Except for methods that simply return true or false due to simple results, other methods should return different values.

To pass to the next step of the workflow for further processing.

In HTML forms, uniformly do not set the name attribute (name) of the submit button.

The element names in HTML forms should be consistent with the database fields.

One statement per line.

Do not use the default method to test non-zero values; you must test explicitly. Example:

if ( false != $this->IsAccountOk() )
{
    ...
}
else
{
    ...
}

Do not use the following style. Example:

if ( $this->IsAccountOk() )
{
    ...
}
else
{
    ...
}

Do not use the ternary logical operator ? :, except for variable assignment. Example:

$_GET['act']   = !empty( $_GET['act'] ) ? $_GET['act'] : 'v_login';

Uniformly use <?php ?>, and it is forbidden to use the <? ?> format.

For get, post, and session type variables, they must be defined and invoked using $_GET, $_POST, and $_SESSION.

Use single quotes '' as much as possible instead of double quotes "".

After use, array variables, object variables, and query result sets must immediately release resources using unset() or free_result().

A PHP file can only contain one class definition, with the class name as the file name.

PHP files must never contain HTML statements, and HTML files should avoid PHP statements as much as possible.

HTML files must pass the W3C HTML4 validation certification (http://validator.w3.org/).

If you find that only a few of your names in the program can match their corresponding things, please redesign the system.

Before naming a class, first know what it is. If you still cannot recall what the class is from the clues provided by the class name, then your design is not good enough.

Mixed names composed of more than three words can easily cause confusion among the entities in the system. Please redesign the class.

Usually each method only performs one logical action or transaction, so their names should clearly explain what they do: use checkForErrors() instead of errorCheck(), use dumpDataToFile() instead of dataFile().

This makes functions and data more distinguishable objects.

V. Program Comments Section

Class comments

/*
* Purpose:
* 作为通用接口访问数据库的类
* Class Name:
* Database
* Author:
* [email protected]
* Modifications:
* 2004-08-18 09:15
* 添加db_insert_id()方法
* [email protected]
* See: 参照说明
*/
class Database
{
    ……
}

Class method comments

/*
* Purpose:
* 取得上一步数据库insert操作产生的id
* Method Name:
* getInsertId()
* Parameter:
* Return:
* int
* Author:
* unixdotnet [email protected]
* Modifications:
* 2004-08-18 09:15
* 修改原因
* unixdotnet [email protected]
* See: 参照说明
*/
function getInsertId()
{
    ...
}

Class property comments

class Database
{
/*
* Purpose:
* 连接数据库所需的用户名
* Attribute Name:
* $databaseUserName
* Type: string
* Author:
* unixdotnet [email protected]
* Modifications:
* 2004-08-18 09:15
* 修改原因
* unixdotnet [email protected]
* See: 参照说明
*/
var $databaseUserName;
    ...
}

VI. Database Application Section

Database design must comply with the three normal forms (except where extremely demanding high-speed requirements apply, in which case separate record tables may be considered).

Database names should be composed of lowercase English nouns that summarize the project content.

Data table names should be composed of lowercase English nouns for the object names (as much as possible corresponding to the business class names in the system).

Use underscores to separate words, avoiding possible case errors when crossing platforms.

Data table fields should avoid variable-length types such as varchar and text. Time information fields should be stored using the unix timestamp type.

When querying data, the use of the * wildcard is forbidden to avoid consuming resources and to speed up processing. Try to avoid using temporary tables.

When querying data with joins across multiple tables, all resources should use full names, i.e., tableName.fieldName, rather than fieldName.

SQL statements should conform to the ANSI92 standard as much as possible, and avoid using database-specific extended features of SQL.

After development is completed, indexes must be added to the condition part (where) of SQL query statements. For queries matching multiple conditions, a composite index should be used.

The composition of the index should match the order of the condition statements from left to right.

It is strictly forbidden to add indexes blindly, to avoid slowing down data insertion speed, increasing occupied space, and slowing down query speed.

Whenever a structural change occurs in the database (table), it must be registered and saved; the database structure and its data must be backed up regularly (within no more than three working days).

1. When assigning a string to a PHP variable, try to use single quotes. Single quotes are much faster.

2. When assigning a value to a PHP variable, if the value contains variables, you have to use double quotes. Double quotes can automatically parse variables, which is much more convenient.

Example: $b=blue; $a="php$b"; echo $a; outputs phpblue (single quotes and double quotes each have their strengths)

3. Try to use double quotes in HTML. No matter how long it is, just add single quotes at the beginning and end when putting it into PHP, do not be afraid of errors.

Original: http://www.phpboke.com/php-standard.html