Pear Introduction: Use Pear to write your next PHP program _php tutorial

Source: Internet
Author: User
Tags dsn pear

Pear Introduction: Use Pear to write your next PHP program


Content:

What is Pear
Why use pear?
What benefits can pear bring to me?
Coding Rules for Pear
Getting Started with pear
Summarize
Related Resources
About the author


Panfan (Night Sailer) (nightsailer@hotmail.com)
Beijing Sadie Network Information Technology Co., Ltd.
June 2001
You may already be a veteran of PHP and write a lot of great code. But if you want to add them to your current project now, are you struggling? Your friend wants to use your code as a module in his project, but you find that you're using a different coding style, adapting it, or even rewriting one!
Please come with me, use the pear standard to write your PHP program, your program will have more vitality, your program and code will be very convenient and other master code together, pear like CPAN for Perl, will allow PHP to generate higher energy.
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);


In addition, if you want to assign a value to the returned result of a 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:
The function definition follows the custom of "one True brace":
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:
Use it at all times Define your PHP code instead of simply using the , this can ensure the compatibility of pear, and also facilitate 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 statement:
/* 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.netso 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 "http://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 prefix the package or class name where the constants are located. For example, constants in a bug class should start with Bug_. The above is the coding rules of pear, detailed coding rules can refer to the description of the Coding_standdard file in pear. To better understand these coding rules, you can also refer to the code of the existing Pear core module.
Getting Started with pear
Pear
Using pear is simple, you just need to define your own pear program:
Require_once "pear.php";
Class Your_class_name extends pear{
Your class definition ...
}


Of course, you need to follow the pear coding rules that you said earlier, and then you can implement what you want to do inside your class. Let's start with a discussion, in fact pear provides us with 2 predefined classes:
Pear: This is the base class for pear, and all pear extensions are derived from its inheritance.
Pear_error:pear the base class for error handling, you can choose to derive your own error-handling class.
In general, you should not create an instance of pear directly, but instead create a new class yourself, and then recreate the instance of the new class. As a base class, Pear provides us with some useful functions, most notably destructors and error handling.
Destructors
PHP supports constructors, but does not support destructors, but PHP provides the register_shutdown_function () function, which enables callbacks to registered functions before the script terminates, so Pear uses this feature to provide a simulation of the destructor. If you have a pear subclass called Mypear, then in Mype

http://www.bkjia.com/PHPjc/486627.html www.bkjia.com true http://www.bkjia.com/PHPjc/486627.html techarticle Pear Introduction: Use Pear to write your next PHP program content: What is pear why use pear? What benefits can pear bring to me? Pear Coding Rules ...

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