PHP Pear Introduction _php Tutorial

Source: Internet
Author: User
Tags dsn

What is Pear

Pear is an abbreviation for the PHP extension and application library (the PHP Extension and application Repository). It is a PHP extension and application of a code warehouse, in short, pear is the PHP cpan.


Why use pear?

PHP is a very good scripting language, concise and efficient, with the release of 4.0, more and more people use it for dynamic website development, it can be said that PHP has become one of the best Internet development language, especially for those who need to be able to quickly, PHP is the preferred language for developers of web sites that develop small-to-medium-sized business applications efficiently. But with the increasing application of PHP, the lack of uniform standards and effective management of these applications, it is difficult for the PHP community to share each other's code and applications as easily as people in the Perl community, Because PHP lacks a unified code base like CPAN to classify and manage the code modules of the application (everyone familiar with Perl knows that Cpan is a huge Perl extension module repository, the application modules can be placed under the appropriate directory under CPAN, others can be easily reused, Of course, you also need to follow the guidelines when you write application modules. )

For this reason, pear came into being and, starting with 4.04, was distributed along with the PHP core.

What benefits can pear bring to me?

1. As mentioned earlier, Pear manages the Pear application code base according to a certain classification, your pear code can be organized into the appropriate directory, others can easily retrieve and share your results.

2.PEAR is not just a code warehouse, it is also a standard, using this standard to write your PHP code, will enhance the readability of your program, reusability, reduce the chance of error.

3.PEAR provides you with 2 classes to build a framework that implements such functions as destructors, error trapping, which you can use with inheritance.


Coding Rules for Pear

Pear's coding rules include indentation rules, control structures, function calls, function definitions, annotations, including code, PHP tags, file header comment blocks, CVS tags, url samples, and constant naming of these 11 aspects. The following is a brief introduction to:

Indentation Rules:
You need to use 4 spaces in pear to indent the code without using tab. If you use Vim, put the following settings in your ~/.VIMRC: set Expandtab
Set shiftwidth=4
Set tabstop=4

If you use Emacs/xemacs, you need to set Indent-tabs-mode to nil.

But you like me to use (X) Emacs to edit php files, I highly recommend you to install Php-mode, so when you write pear code, it will automatically adjust your indentation style, of course, php-mode there are many excellent features, You can download the latest version of Php-mode from the resources list.

Control structure:
The control structure described here includes the following: If for and while switch. For control structures, in keywords such as if for. The next one is empty, and then the parentheses are controlled, so that it is not confused with the function call, and you should try to use the curly brace {} as fully as possible, even if it is syntactically optional. This prevents you from having to add new lines of code in the future to produce logical doubts or errors. Here is a sample: if (condition 1) && (condition 2)) {
Statement 1;
}esleif (condition 3) | | (condition 4)) {
Statement 2;
}else {
Statement 3;
}

Function call:
For function calls, there should be no spaces between the functions name and the opening parenthesis (there should be no space between the separated comma and the next argument, and no space between the last argument and the closing parenthesis). The following is a standard function call; $result = foo ($param 1, $param 2, $param 3);
Non-canonical notation:
$result =foo ($param 1, $param 2, $param 3);
$result =foo ($param 1, $param 2, $param 3);

Also, if you want to assign a value to the returned result of the function, there is a space between the equal sign and the assigned variable, and if it is a series of related assignment statements, you add the appropriate spaces to align them, like this: $result 1 = $foo ($param 1, $param 2, $param 3 );
$var 2 = $foo ($param 3);
$var 3 = $foo ($param 4, $param 5);

function definition:
function definition follows "One true brace" Custom: Function Connect (& $DSN, $persistent = False)
{
if (Is_array ($DSN)) {
$dsninfo = &&dsn;
} else {
$dsninfo = DB::p arsedsn ($DSN);
}
if (! $dsninfo | |! $dsninfo [Phptype]) {
return $this->raiseerror ();
}
return true;
}

As shown above, optional parameters are at the end of the parameter table and always try to return meaningful function values.

About Comments:
The online documentation for the class should be able to be converted by Phpdoc, just like Javadoc. Phpdoc is also a pear application, more detailed introduction you can go to http://www.phpdoc.de/view. In addition to the class of online documentation, it is recommended that you use non-document-nature annotations to interpret your code, and when you see a piece of code, think: Oh, I don't think I need to describe it in a document. Then you'd better make a simple comment on the code so that you don't forget how it works. In the form of annotations, c/*/and C + +//are good, however, do not use Perl or the Shell's # notation.

Include Code:
Whenever you need to include a class file unconditionally, you must use requre_once; When you need to include a class file, you must use include_once, which will ensure that the file you want to include is included only once, And these 2 statements share the same file list, so you don't have to worry about the two being confused, and once require_once contains a file, Include_once won't repeat the same file, and vice versa.

PHP Code Tags:
Always use the definition of your PHP code instead of simply using it, which guarantees pear compatibility and facilitates cross-platform porting.

Comment Declaration for file header:
All PHP code files that need to be included in the Pear core, at the beginning of the file, you must add the following comment declaration:/* Vim:set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2001, the PHP Group |
// +----------------------------------------------------------------------+
// | This source file was subject to version 2.0 of the PHP license, |
// | That's bundled with the "This" file LICENSE, and is |
// | Available at through-World-wide-web at |
// | Http://www.php.net/license/2_02.txt. |
// | If you do not receive a copy of the PHP license and is 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 $

For files that are not in the Pear core code base, it is recommended that you also have a similar comment block at the beginning of the file that identifies the copyright, the agreement, the author, and so on. It also joins Vim's modeline in the first line, so that it can maintain pear code style in vim.

CVS Tags:
As shown above, include the CVS ID tag in each file, if you edit or modify the file does not have this tag, then please join, or replace the original file similar expressions (such as "last Modified" and so on)

URL Sample:
You can refer to RFC 2606 and use "www.example.com" as a sample of all URLs.

Constant Name:
Constants should be capitalized as much as possible, to make it easier to understand, use underscores to split each word. At the same time, you should have a constant in the package name or

http://www.bkjia.com/PHPjc/486626.html www.bkjia.com true http://www.bkjia.com/PHPjc/486626.html techarticle What is pear pear is an abbreviation for PHP extensions and Applications library (the PHP Extension and application Repository). It is a code repository for PHP extensions and Applications ...

  • 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.