Introduction to PHP PEAR_PHP Tutorial

WBOY
Release: 2016-07-13 17:36:47
Original
873 people have browsed it

什么是PEAR

PEAR是PHP扩展与应用库(the PHP Extension and Application Repository)的缩写。它是一个PHP扩展及应用的一个代码仓库,简单地说,PEAR就是PHP的CPAN。


为什么要使用PEAR?

PHP是一个非常优秀的脚本语言,简洁、高效,随着4.0的发布,越来越多的人使用它来进行动态网站的开发,可以说,PHP已经成为最优秀的INTERNET开发语言之一,尤其对于那些需要能够快速、高效地开发中小规模的商业应用的网站开发人员,PHP是其首选的语言。但是随着PHP的应用的不断增多,对于这些应用缺乏统一的标准和有效的管理,因此,PHP社区很难象PERL社区的人们那样方便的共享彼此的代码和应用,因为PHP缺乏象CPAN那样的统一的代码库来分类管理应用的代码模块(熟悉PERL的人都知道,CPAN是一个巨大的PERL的扩展模块仓库,编写的应用模块可以放在CPAN下面的适当的分类目录下面,其他的人可以很方便地复用,当然,你编写应用模块时候也需要遵守其中的准则。)

为此,PEAR就应运而生了,并且从4.04开始,随着PHP核心一起被分发。

 

PEAR能给我带来什么好处?

1.如前所述,PEAR按照一定的分类来管理PEAR应用代码库,你的PEAR代码可以组织到其中适当的目录中,其他的人可以方便地检索并分享到你的成果。

2.PEAR不仅仅是一个代码仓库,它同时也是一个标准,使用这个标准来书写你的PHP代码,将会增强你的程序的可读性,复用性,减少出错的几率。

3.PEAR通过提供2个类为你搭建了一个框架,实现了诸如析构函数,错误捕获功能,你通过继承就可以使用这些功能。


PEAR的编码规则

PEAR的编码规则包括缩进规则,控制结构,函数调用,函数定义,注释,包含代码,PHP标记,文件头的注释块,CVS标记,URL样例,常量的命名这11方面。下面简要地介绍一下:

缩进规则:
PEAR中需要使用4个空格来缩排代码,并且不使用TAB。如果你使用VIM,将下列设置放入你的~/.vimrc中:set expandtab
set shiftwidth=4
set tabstop=4

如果,你使用Emacs/XEmacs,需要把indent-tabs-mode 设置成nil。

不过你象我一样喜欢用(X)Emacs编辑PHP文件,我强烈推荐你安装PHP-MODE,这样当你编写PEAR代码的时候,它会自动调整你的缩排风格,当然PHP-MODE还有许多很优秀的特性,你可以从资源列表中的地方下载最新版的PHP-MODE。

控制结构:
这里所说的控制结构包括: if for while switch 等。对于控制结构,在关键字(如if for ..)后面要空一个格,然后再跟控制的圆括号,这样,不至于和函数调用混淆,此外,你应该尽量完整的使用花括号{},即使从语法上来说是可选的。这样可以防止你以后需添加新的代码行时产生逻辑上的疑惑或者错误。这里是一个样例:if ((条件1) && (条件2)) {
    语句1;
}esleif ((条件3) || (条件4)) {
    语句2;
}else {
    语句3;
}

 

函数调用:
对于函数调用,函数名和左括号( 之间不应该有空格,对于函数参数,在分隔的逗号和下一个参数之间要有相同的空格分离,最后一个参数和右括号之间不能有空格。下面是一个标准的函数调用;$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);

 

函数定义:
函数定义遵循"one true brace"习俗:function connect(&$dsn, $persistent = false)
{
    if (is_array($dsn)) {
        $dsninfo = &&dsn;
    } else {
        $dsninfo = DB::parseDSN($dsn);
    }
    if (!$dsninfo || !$dsninfo[phptype]) {
        return $this->raiseError();
    }
    return true;
}

如上所示,可选参数要在参数表的末端,并且总是尽量返回有意义的函数值。

Regarding comments:
Online documentation of classes 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/ to view it. In addition to 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 you need to describe it carefully in the documentation. Then you'd better give this code a simple comment to prevent you from forgetting how it works. For comment forms, C's /* */ and C++'s // are both good, but do not use Perl or shell's # comment method.

Include code:
Whenever, when you need to unconditionally include into a class file, you must use require_once; when you need to conditionally include into a class file, you must use include_once; This can ensure that you want to include The file will only be included once, and these two statements share the same file list, so you don't have to worry about the two getting confused. Once require_once includes a file, include_once will not include the same file again, and vice versa.

PHP code tag:
Always use to define your php code instead of simply using . This can ensure PEAR compatibility and is also beneficial. Cross-platform porting.

Comment statement in the file header:
For all PHP code files that need to be included in the PEAR core release, you must add the following comment statement at the beginning of the file: /* 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 Your Name |
// +----------------------------------- ----------------------------------+
//
// $Id$

For files that are not in the PEAR core code base, it is recommended that you also have a similar comment block like this at the beginning of the file, indicating copyright, license, author, etc. At the same time, add VIM’s MODELINE in the first line, so that PEAR’s code style can be maintained in VIM.

CVS tag:

As shown above, add the CVS ID tag to each file. If the file you edit or modify does not have this tag, please add it, or replace it with something similar in the original file. Expression form (such as "Last modified", etc.)

URL samples:

You can refer to RFC 2606 and use "www.example.com" as all URL samples.

Constant naming:

Constants should be in uppercase letters as much as possible. For ease of understanding, use underscores to separate each word. Also, you should specify the package name where the constant is located or

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486626.htmlTechArticleWhat is PEAR? PEAR is the abbreviation of PHP Extension and Application Repository. It is a code repository for PHP extensions and applications...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template