PEAR's coding rules include indentation rules, control structures, function calls, function definitions, comments, containing code, PHP tags, comment blocks in file headers, CVS tags, URL samples, and naming of constants. Here is a brief introduction
apt-get install php-pear
pear install channel://pear.php.net/PHP_ArrayOf-0.2.1
pear download channel://pear.php.net/PHP_ArrayOf-0.2.1
PEAR coding rules
PEAR coding rules include indentation rules and control structures , function calls, function definitions, comments, 11 aspects including code, PHP tags, comment blocks in file headers, CVS tags, URL samples, and naming of constants. Here is a brief introduction:
Indentation rules:
PEAR requires 4 spaces to indent the code, and no TAB is used. If you use VIM, put the following settings into your ~/.vimrc:
set expandtabset shiftwidth=4set tabstop=4
If you use Emacs/XEmacs, required Set indent-tabs-mode to nil.
But if you like to use (X)Emacs to edit PHP files like me, I strongly recommend you to install PHP-MODE so that when you write PEAR code, it will automatically adjust your indentation style. Of course PHP-MODE also has many excellent features. You can download the latest version of PHP-MODE from the resource list.
Control structure:
The control structures mentioned here include: if for while switch, etc. For control structures, there should be a space after the keyword (such as if for ..), and then the control parentheses, so that it will not be confused with function calls. In addition, you should try to use curly braces {} as completely as possible. Even if it is syntactically optional. This will prevent logical confusion or errors when you need to add new lines of code in the future. Here is an example:
if ((Condition 1) && (Condition 2)) { Statement 1;}esleif ((Condition 3) || (Condition 4)) { Statement 2;}else { Statement 3;}
Function call: There should be no space between the function name and the left bracket ( ( ), and for function parameters, between the delimiting comma and the next parameter There must be the same space separation between them, and there must be no space between the last parameter and the right bracket. The following is a standard function call;
$result = foo($param1, $param2, $param3); 不规范的写法: $result=foo ($param1,$param2,$param3);$result=foo( $param1,$param2, $param3 );
$result1 = $foo($param1, $param2, $param3); $var2 = $foo($param3); $var3 = $foo($param4, $param5);
About comments:
For online documentation of classes, it should be able to be converted by PHPDoc, just like JavaDoc. PHPDoc is also a PEAR application. For a more detailed introduction, you can go to http:/ /www.phpdoc.de/ View. In addition to the online documentation of classes, it is recommended that you use non-documentation comments to explain your code. When you see a piece of code, you think: Oh, I don’t think so. It needs to be described carefully in the documentation. Then you'd better make a simple comment for this code to prevent you from forgetting how they work. For the form of comments, C's /* */ and C's / / are fine, but don’t use Perl or shell’s # comment method.
Include code:
Whenever you need to unconditionally include a class file, you must Use require_once; when you need to conditionally include a class file, you must use include_once; this ensures that the file you want to include will only be included once, and these two statements share the same file list, so you don’t have to worry about confusion between the two. , once require_once includes a file, include_once will not include the same file again, and vice versa.
PHP code tags:
Always use Define your PHP code instead of simply using ?>. This can ensure the compatibility of PEAR and also facilitate cross-platform porting.
Comment statement in the file header:
/* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Original Author | // | Your Name | // +----------------------------------------------------------------------+ // // $Id$
对于不在PEAR核心代码库中的文件,建议你也在文件的开始处有这样一个类似的注释块,标明版权,协议,作者等等。同时也在第一行加入VIM的MODELINE,这样在VIM中能够保持PEAR的代码风格。
CVS标记:
如上面所展示那样,在每个文件中加入CVS的ID标记,如果你编辑或修改的文件中没有这个标记,那么请加入,或者是替换原文件中相类似的表现形式(如"Last modified"等等)
URL样本:
你可以参照RFC 2606,使用"www.example.com"作为所有的URL样本。
常量命名:
常量应该尽量使用大写,为了便于理解,使用下划线分割每个单词。同时,你应该常量所在的包名或者是类名作为前缀。比如,对于Bug类中常量应该以Bug_开始。
以上是PEAR的编码规则,详细的编码规则可以参考PEAR中的CODING_STANDDARD文件的说明。为了更好地理解这些编码规则,你也可以参考一下现有PEAR核心模块的代码。
推荐学习:php视频教程
The above is the detailed content of How to use pear extension package in PHP. For more information, please follow other related articles on the PHP Chinese website!