PHP Programming Specification

Source: Internet
Author: User
Tags array definition
I have always been in the style of PHP functions to write PHP, all variables, functions, classes are used lowercase, the word between the lines separated, has been compared to exclude the Hump code specification, the person think in the case of the letter between the writing code, very troublesome, and PHP's own functions are lowercase, Why don't I use this format?

Good code writing habits + good commenting habits + Phpdocumentor = Program Instructions

A team, must have a neat code writing habits, if a unified IDE development environment, detailed task coding process, perfect code testing (such as: SimpleTest), then the overall team development efficiency will be greatly improved.

If your IDE is eclipse, then you can easily test the code, using SimpleTest reference http://www.guogoul.com/2008/05/19/simpletest_1/

Description: This specification by Easychen for reference Sina Network application Development Department "C + + Development Code" and the interactive technology department "PHP4 Development Code", as well as the Phpdocument code to collate the development norms. I feel very good, suitable for PHP development. For everyone's reference, it is necessary to develop a good programming style. Reproduced

1th. Naming specification 1.1 variables 1.1.1 Global variables
Global variables start with $g_, such as $g_data_list.

1.1.2 General Variables
The general variables are named with lowercase letters, and the words are separated by underscores.
Variable names should use nouns or adjectives + nouns. such as $value, $new _value.

1.1.3 Temp Variable
Do not use temporary variables such as $i, $J, etc. that are frequently used in loops for other purposes.

1.2 Functions
The function is named with lowercase letters, and the words are separated by underscores.
The naming of functions suggests the use of verbs + nouns, such as get_user_img.
A function that completes a set of functions is put into a file, and the file holding the function is named function_name.func.php.

Class 1.3
Class uses the case of English to separate words, including the first word, all words capitalized, such as PageManager;
In a class, the method is placed before the attribute definition, and the public method is placed in front of the private method;
In general, a class corresponds to a file;
When some classes are closely related, they can be stored in a file;
The file that holds the class is named by the ClassName.class.php method.

1.4 Methods
Method use the case of English to separate words, except the first word, the first letter of other words capitalized, such as Getcurrentpage ();
Do not use abbreviations that are not commonly used, such as where2go ();
When using a commonly used abbreviation, capitalize only the first letter, such as gethtml ().

2nd Chapter Layout Rules

2.1 Semantic separation
Each function, method should adopt the empty line interval;
A close-knit statement in the same function can be non-wrapped, and other cases require a newline.

2.2 Space Rules

2.2.1 Logical operators must be preceded by a space
Correct $a = = $b;
Error $a = = $b;
$a = = $b;
Notes

Correct $a + +; $a?;
Error $a + +; $a?;
Note adding one minus one operator cannot add a space.

2.2. More than 2 parameters must be separated by a space
Correct $g _pro, $g _user, g_show;
Get_db_info ($host, $user, $passwd);
Error $g _pro, $g _user, $g _show;
Get_db_info ($host, $user, $passwd);
Notes

2.2.3 Syntax keyword must be added with a space
For example: If, for, while, switch .....

Correct for ($i = 0; $i < $i + +)
Error for ($i = 0; $i < $i + +)
Notes

2.3 String and variable connection rules
strings are connected to variables using '. ' Must be in the '. ' Before and after adding a space, use the "number", you must add "{}" before and after the variable.

Correct $my _name = ' file_ '. $var 1;
$my _name = "file_{$var 1}";
Error $my _name = "File_". $var 1;
$my _name = "File_$var1″;
Notes

2.4 Parenthesis Rules
The parentheses of the function name do not need spaces, and the parentheses after the syntax keyword must be blank.

Correct for ($i = 0; $i < $i + +)
strlen ($my _name);
Error for ($i = 0; $i < $i + +)
strlen ($my _name);
Notes

2.5 curly Braces Rule
Curly braces must correspond to the top and bottom.

That's right
if ($a)
{
$b = $a;
}

Error if ($a) {
$b = $a;
}

Notes

2.6 Array Definition Rules

The array definition and use must be preceded by a single quotation mark before and after the key value.

PHP Code:

Download: php_array.php//correct
Array
' Name ' = ' d5s.cn ',
' Gender ' = ' php '
);
Error
Array
name = ' d5s.cn ',
gender = ' php '
);
?>

2.7 SQL rules

All SQL statement keywords embedded in PHP are capitalized;
Table names and field names are enclosed in anti-quotation marks (') to prevent errors that occur because the field names contain spaces;
The data values are enclosed in single quotation marks, and you should ensure that the single quotes in the data values are escaped to prevent SQL injection.

Correct $sql = "Select ' user '. ' Name ' from ' user ' WHERE ' id ' = ' $id ' LIMIT 1″;
Error $sql = "Select Name.user from name where id = $id";
Notes

3rd Chapter Rules of annotations

3.1 General rules
Do not write unnecessary comments, but only if the code does not describe the logic well, it is supplemented with annotations;
Consider annotations as part of a program and write/maintain comments while writing/maintaining code;
Annotations take full phpdocumentor specifications to facilitate the generation of API-level documentation.

3.2 Detailed rules
Please refer to the Phpdocumentor manual. Below is a demonstration of the annotations for each section.

3.2.1 Copyright Information
Comment Name Copyright information
Annotation Demonstration//
// +????????????????? -+
// | Phpdocumentor |
// +????????????????? -+
// | Copyright (c) 2000-2003 Joshua Eichorn |
// | Email jeichorn@phpdoc.org |
// | Web http://www.phpdoc.org |
// +????????????????? -+
// | This source file was subject to PHP License |
// +????????????????? -+
//
Note Use//to mark the copyright information, in order to avoid conflicts with Phpdocumentor Page-level DocBlock

3.2.2 File Header Comment Example

Comment Name File header comment
Demonstration of annotations

Download: php_doc.php * All abstract representations of inline tags is in this file
* @package Phpdocumentor
* @subpackage Inlinetags
* @since separate file since version 1.2
* @version $Id $
*/
?>

Note
1 File header comments need to indicate the owning package and child package
2 Add $id to @version to facilitate the use of CVS to manage files

3.2.3 Class Comment Example
Comment Name Class comment
Demonstration of annotations

Download: php_class.php/**
* Use this element to represent a {@}inline tag} like {@}link}
* @see Parserstringwithinlinetags
* @package Phpdocumentor
* @subpackage Inlinetags
* @author Greg Beaver
* @since 1.0RC1
* @version $Revision: 1.21.2.6 $
* @tutorial inlinetags.pkg
*/
?>

Notes

3.2.4 Class Property Comment Example
Comment Name Class Property comment
Demonstration of annotations

Download: php.php/** var $type = ' Inlinetag ';
* Element Type
*
* Type is used by many functions to skip the hassle of
*
*
* If Get_class ($blah) = = ' Parserblah '
*
* Always "Inlinetag"
* @var String
*/
?>

Notes

3.2.5 function/class method Comment example
Comment Name function/class method comment
Demonstration of annotations

Download: php.php/** function getString () ";
* @return string Always '
* Calculate the short description of a DocBlock
* @see parserstringwithinlinetags::getstring ()
* @see Parserstringwithinlinetags::trimmedstrlen ()
*/

{
Return
}
?>

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.