Internal use of drupal/php development track 1.2

Source: Internet
Author: User
Tags array definition doxygen drupal
Code for development of internal drupal/php 1.2
Objective
As the saying goes, "no rules, inadequate surrounding area." Good programming styles and specifications are important for developers as well as for project managers.
When a software project tries to comply with common standards, it makes it easier for the project's developers to understand the code in the project and clarify the state of the program. So that the new participants can quickly adapt to the environment, to prevent some participants to save time for the need to create a set of style and develop lifelong habits, causing others to waste too much time and energy in reading. And in a consistent environment, you can also reduce the chance of coding errors. The flaw is because each person's standard is different, therefore needs a period of time to adapt and changes own coding style, temporarily reduced the bottom the work efficiency. It is worthwhile to consider the temporary reduction of work efficiency from the development of long-term health of the project and the higher team work efficiency in the later period, and it is a process that must go through. Standards are not the key to the success of a project, but they can help us to be more efficient in team collaboration and to accomplish the tasks we have set out.

1th chapter, typesetting rules

1.1. Indent
The code indentation uses 2 spaces instead of the TAB key, because the TAB key's length root is different when viewing the code in various editors, although the spaces increase the size of the file, and these errors are trivial.
In many editors, you can set the TAB key to 2 spaces if you are accustomed to using the TAB key.

1.2. Space rules
Spaces should be used in the following situations:

1. Keywords and (there should be 1 spaces, for example:
while ($user->uid > 0) {
?>
2. Function names and (there should be no spaces between them, for example:
function arg () {
?>
3. There should be no space between the unary operator and its operand, unless the operand is a word, for example: typeof.
4. Each in the control section, example for statement, ";" Must be followed by a space.
5. Each, after should be followed by a space.

1.2.1, logical operators must be preceded before and after a space, plus one minus one operation except
That's right
$a = = $b;
Error
$a = = $b;
$a = = $b;
That's right
$a + +;
$a--;
Error
$a + +;
$a--;
?>



1.2.2, multiple arguments must be separated by a space
That's right
$a, $ab, $c;
Arg ($a, $b, $c);
Error
$a, $ab, $c;
Arg ($a, $b, $c);
?>



1.2.3, syntax keyword must be followed by a space
If, for, while, switch and other keywords.
Cases:
That's right
for ($a = 0;$ <; $a + +);
Error
for ($a = 0;$ <; $a + +);
?>



1.3. Connection rules for strings and variables
The string is connected to the variable using "." And in the "." There are 1 spaces left and right, "{}" must be added before and after the variable when using "Auto escape variable"
That's right
$result = ' File_ '. $var;
$result = ' File_{var} ';
Error
$result = ' file_ '. $var;
$result = ' File_$var ';
?>



1.4. One statement per line, unless the statements are closely related, each line is written with only one statement. Use a blank line to separate logically related statements to improve the readability of the program.
That's right
$node _types = node_get_types (' Types ', NULL, TRUE);

foreach ($node _types as $type = + $info) {
//
}
?>


Error
$node _types = node_get_types (' Types ', NULL, TRUE);
foreach ($node _types as $type = + $info) {
//
}
?>



1.5. Key words
If, for, does, while, case, switch, default, and so on, and so on, and if, for, does, while and other statements of the execution of the statement part of the statement, no matter how much must have full parentheses {}
Cases:
That's right
if (NULL = = $arg) {
Return
}
?>


Error
if (NULL = = $arg) return;
?>



1.5.1, if
The format is as follows:
if (condition) {
//
}

if (condition) {
//
} else {

}

if (condition) {
//
} else {
//
}
?>



1.5.2, for
The format is as follows:
for ($i = 0; $i < 1; $i + +) {
//
}
?>



1.5.3, do
The format is as follows:
Unlike other compound statements, do statements are always ";" End.
do {
//
} while (condition);
?>



1.5.4, do
The format is as follows:
switch (expression) {
Case expression:
statements;
Default
statements;
}
?>


Each group of statements (except default, should end with a break, return, or throw.) Don't let it go down in the next step. )



1.5.5, try
The format is as follows:
try {
Statements
} catch (variable) {
Statements
}
?>



1.6. Database

* The keyword portion of the query statement should be capitalized.

2nd Chapter, naming specification
Naming unified adherence to Linux C naming conventions, including PHP, XHTML, CSS, JS.
Variable name All letters are lowercase, use "_" as the demarcation of each word (CSS uses "-", because a lot of Drupal generated code with "-" (such as the block ID, usually similar to #block-vacation). ), the variable name must have a certain meaning, make the program at a glance.
In the project, the variable name of the same meaning should be the same as possible, for example: Drupal project $node represents nodes, nodes are used in many places, if the name is the same, you can know that this is a node, the use of node-related function operations, such as to distinguish multiple nodes, you can add meaningful letters in front , example: $book _node, $color _node.

2.1. Constants
PHP constants should all use uppercase letters, and use _ to correctly delimit words.
Constants use the module name as a prefix, which avoids collisions with characters commonly used words. Example: Your module name is Test.module, then the constant name should be as example Test_user.
Define (' Test_confirm_uid ', 0);
?>



2.2. Global variables
Global variable naming rules: $_ module name (subject name) _ Global variable name
Cases:
Error
Globe $records;
That's right
Globe $_test_records;
?>



2.3. Static variables
Static variables use the prefix s, for example:
$s _records;
?>



2.4. Temporary variables
Do not use temporary variables that are frequently used in loops such as $i, $j, and so on for other purposes.

2.5. Function naming
function name in C gun Convention, all letters using lowercase letters, rules: module name +_+ function name, note that the name should not be named with the relevant hooks of Drupal.

2.6. File name naming
The file names should all be lowercase. The exception is the document files, which are all uppercase and type TXT files.
For example:
LICENSE.txt
README.txt
INSTALL.txt

3rd Chapter, Programming specification
The system uses timestamp time () as the time flag, and writes using the Int (10) type when it is stored in MySQL.
Quotation marks use single quotes, and double quotes are used only when the quotation marks overlap, so that each process can save hundreds of K of memory.
Unified use
Prohibit the use of ?>



3.1. Array definition Rules
Key, value must use single/double quotation marks.
That's right
Array
' Name ' = ' myname ',
' Type ' = ' mytype '
);
Error
Array
Name = MyName,
Type = MyType
);
?>



3.2, do not use the default method to test the variable/function
That's right
if ($user > 0) {
//
}

if (ARG (1)! = ") {
//
}
Error
if ($user) {
//
}

if (ARG (1)) {
//
}
?>



3.3. Document Structure rules
The Drupal module development rules are as follows (for example, module named book):
The main module (book.module) contains Hook_help (), Hook_init (), Hook_menu (), and some of the common functions of the module, such as functions that require calls to other modules (except for core modules that must be enabled). You should first determine if the module is open (example: module_exists (' book ');).
Book.admin.inc (Manage page-related code), Book.page.inc (code used for user actions), should be classified and placed, using the "file" key in Hook_menu to point to.
Some JS plugins used in the module, such as jquery plugins, should be placed in the JS folder in the module directory.

3.4. Database Operation rules

* When querying 1 and multiple records, use Db_query_range ().
* Each query should be added with order for index ordering.

3.5. Comments
Note the document follows the Doxygen comment style and the comment block syntax is as follows (including CSS, JS):
/**
* Implementation of Hook_cron ().
*/
function Node_cron () {
Db_query (' DELETE from {history} where timestamp <%d ', node_new_limit);
}
?>


The comment block must be placed immediately before the function, and there is no blank line in the middle.
Drupal is able to understand the Doxygen constructs listed below, depending on the structure of the Doxygen document:



* @mainpage
* @file
* @defgroup
* @ingroup
* @addtogroup
* @param
* @return
* @link
* @see
* @{
* @}

3.5.1,//$id $
"//$id $" is used to track the version number and the last modified user, with comments placed at the forefront of each file (including JS, CSS, etc. all files that need to be submitted to the repository). When the code is submitted to the CVS (SVN), the system will automatically parse and extend the tag, which becomes as follows:
$Id: node.module,v 1.947.2.29 2010/12/15 12:53:33 goba Exp $
?>



The 4th chapter, writing Security Code
4.1. Handling user input and output
Non-Drupal Hook_form generated forms (including some Ajax) submitted on the data should be securely filtered. Below is a list of some of the filtering functions provided by Drupal.

* Check_plain (HTML), converted to plain text, converts a specific character to an HTML entity.
* FILTER_XSS (HTML), use a set of tags, check and clean HTML
* Check_markup (HTML), filter (can be customized) using filters
* Drupal_urlencode (URL), encodes a specific character as%0x
* Check_url (URL), clean up URLs in harmful protocols, such as JAVASCRIPT:RUNEVILJS ()

Typically in the output, the T () function should be used to filter the output to support Drupal's multi-language modules.
4.2. Handling MySQL Statements
For database additions and deletions to the operation, all use Db_query () function (Drupal7 use change, refer to relevant data), the table name must use curly braces, so when there is a database prefix, you can automatically judge. As follows:
/**
* Implementation of Hook_cron ().
*/
function Node_cron () {
Db_query (' DELETE from {history} where timestamp <%d ', node_new_limit);
}
?>



4.2.1, using Db_rewrite_sql () to maintain private data privacy
If the node Access module is enabled to control the access of the node, then the data detected directly through the Db_query () is unable to filter the permissions, at this time, the need to use the Db_rewrite_sql (), in some node data operation, if the MySQL statement did not process to the Permissions section , you must use this function. Cases:
$result = Db_query (Db_rewrite_sql ("select DISTINCT b.* from {blocks} B left JOIN {blocks_roles} r on b.module = R.module A ND B.delta = R.delta WHERE b.theme = '%s ' and b.status = 1 and (R.rid in (". Db_placeholders ($rids). ") OR R.rid is NULL) Order by B.region, B.weight, B.module ", ' B ', ' bid '), Array_merge (Array ($theme _key), $rids));
?>



Chapter 5th, other important miscellaneous

* Do not modify the Drupal core code as a last resort, and should be documented in the development documentation, if modified.
* The HTML Display section of the module uses the default style as much as possible, such as table (Theme (' Table ', $header, $rows)), List (Theme (' Item_list ', $items)), etc., such as the Special Style section, HTML, CSS should be written in the module instead of relying on the theme, so that in the theme of the switch, it will not be unusable.
* All links must use the L () function, which automatically determines the location of the site's root directory and avoids dead links after the site is migrated.
* Access control is tied to the hook_menu as much as possible, and should not be judged too much within the function.
  • 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.