PEAR introduction: Use PEAR to write your next php program _ PHP Tutorial

Source: Internet
Author: User
Tags code tag
PEAR introduction: Use PEAR to write your next php program. Pear introduction: Use pear to write your next php program content: What is pear? why is pear used? What benefits does pear bring to me? Pear Encoding Rules

Pear introduction: Use pear to write your next php program


What is pear?
Why pear?
What benefits does pear bring to me?
Pear Encoding Rules
Start using pear
Related Resources
Author profile

Panfan (night sailer) (
Beijing Saidi Network Information Technology Co., Ltd.
June 2001
You may already be a veteran of php and have written a lot of very good code. However, if you want to add them to your current project, is it difficult? Your friend wants to use your code as a module in his project, but you find that you have used a completely different encoding style to adapt to it. it is better to rewrite it!
Please come with me and use the pear standard to compile your php program. your program will be more dynamic, your programs and code will be easily integrated with other Masters' code. pear, like cpan's for perl, will make php generate higher energy.
What is pear?
Pear is the abbreviation of the php extension and application repository. It is a code repository for php extensions and applications. In short, pear is the cpan of php.
Why pear?
Php is a very good scripting language that is concise and efficient. with the release of 4.0, more and more people use it to develop dynamic websites. it can be said that, php has become one of the best internet development languages. especially for website developers who need to develop small and medium-sized commercial applications quickly and efficiently, php is the preferred language. However, with the increasing number of php applications, there is a lack of unified standards and effective management for these applications. therefore, it is difficult for the php community to share their code and applications as easily as those in the perl community, because php lacks a unified code library like cpan to classify and manage application code modules (everyone familiar with perl knows that cpan is a huge library of perl extension modules, the written application modules can be placed under the appropriate category directory under cpan. other users can easily reuse them. of course, you also need to follow the rules when writing the application module .)
To this end, pear came into being, and has been distributed along with the php core since 4.04.
What benefits does pear bring to me?
1. as mentioned above, pear manages the pear application code library according to certain categories. your pear code can be organized into appropriate directories, others can easily retrieve and share your results.
2. pear is not only a code repository, but also a standard. Using this standard to write your php code will enhance the readability and reusability of your program and reduce the chance of errors.
3. pear builds a framework for you by providing two classes to implement functions such as destructor and error capture. you can use these functions through inheritance.
Pear Encoding Rules
Pear encoding rules include indentation rules, control structures, function calls, function definitions, comments, including code, php tags, file header comments, cvs tags, and url samples, constant naming. The following is a brief introduction:
Indent rules:
In pear, four spaces are required for code reduction, and tab is not used. If you use vim, put the following settings into your ~ /. Vimrc:
Set expandtab
Set shiftwidth = 4
Set tabstop = 4

If you use emacs/xemacs, set indent-tabs-mode to nil.
However, you like to use (x) emacs to edit php files. I strongly recommend that you install php-mode so that when you write pear code, it will automatically adjust your contraction style. of course, php-mode has many outstanding features. you can download the latest php-mode from the resource list.
Control structure:
The control structure mentioned here includes: if for while switch. For control structures, in keywords (such as if ..) leave a space box and then use parentheses to avoid confusion with function calls. In addition, you should try to use curly braces {} as far as possible {}, even if the syntax is optional. In this way, you can avoid logical doubts or errors when adding new code lines. Here is an example:
If (condition 1) & (condition 2 )){
Statement 1;
} Esleif (condition 3) | (condition 4 )){
Statement 2;
} Else {
Statement 3;

Function call:
For function calling, the function name and left parenthesis (there should be no space between them. for function parameters, there must be the same space separation between the separated comma and the next parameter, there must be no space between the last parameter and the right brace. The following is a standard function call;
$ Result = foo ($ param1, $ param2, $ param3 );
Non-standard writing:
$ Result = foo ($ param1, $ param2, $ param3 );
$ Result = foo ($ param1, $ param2, $ param3 );

In addition, if you want to assign values to the results returned by the function, there must be spaces between the equal sign and the assigned variable. In addition, if you are using a series of related values, you should add appropriate spaces, align them like this:
$ Result1 = $ foo ($ param1, $ param2, $ param3 );
$ Var2 = $ foo ($ param3 );
$ Var3 = $ foo ($ param4, $ param5 );

Function definition:
The function definition follows the "one true brace" convention:
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;

As shown above, optional parameters must be at the end of the parameter table and always return meaningful function values whenever possible.
Online documents of classes should be converted to phpdoc, just like javadoc. Phpdoc is also a pear application. For more details, visit In addition to the online documentation of classes, we recommend that you use non-document comments to interpret your code. when you see a piece of code, consider: Oh, I don't need to describe it carefully in the document. So you 'd better make a simple comment on the code to prevent you from forgetting how it works. For the form of annotation, c's/**/and c ++'s // are both good. however, do not use perl or shell's # annotation method.
Code included:
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; this ensures that the files you want to include will only be contained once, and the two statements share the same file list, so you do not have to worry about confusion between the two statements. once require_once contains a file, include_once will no longer contain the same file, and vice versa.
Php code tag:
Always use Define your php code, instead of simply using This ensures pear compatibility and facilitates cross-platform migration.
Annotation declaration of the file header:
All php code files that need to be included in the pear core release. at the beginning of the file, you must add the following comments:
/* Vim: set expandtab tabstop = 4 shiftwidth = 4 :*/
// + ------------------------------------------------------------------------ +
// | Php version 4.0 |
// + ------------------------------------------------------------------------ +
// | Copyright (c) 1997,199 8, 1999,200 0, 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:// |
// | 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.netso we can mail you a copy immediately. |
// + ------------------------------------------------------------------------ +
// | Authors: original author |
// | Your name |
// + ------------------------------------------------------------------------ +
// $ Id $

For files not in the pear core code library, we recommend that you have such a comment block at the beginning of the file to indicate copyright, protocol, author, and so on. At the same time, we also add the vim modeline in the first line, so that we can maintain the pear code style in vim.
Cvs flag:
As shown above, add the cvs id tag to each file. if this tag is not found in the edited or modified file, add it, or replace similar representations in the original file (such as "last modified)
Url sample:
You can refer to rfc 2606 to use "" as all url samples.
Constant name:
Constants should be capitalized as much as possible. to facilitate understanding, use underscores to separate each word. At the same time, you should use the package name or class name where the constant is located as the prefix. For example, constants in the bug class should start with bug. The above are pear encoding rules. for detailed encoding rules, refer to the description of coding_standdard file in pear. To better understand these encoding rules, you can also refer to the code of the existing pear core module.
Start using pear
It's easy to use pear. you just need to define your own pear program as follows:
Require_once "pear. php ";
Class your_class_name extends pear {
Your class definition...

Of course, you need to follow the pear encoding rules mentioned above, and then you can implement what you want to do inside your class. Next, let's discuss it. In fact, pear provides us with two predefined classes:
Pear: this is the base class of pear. all pear extensions must be derived from it.
Pear_error: base class for processing pear errors. you can choose to derive your own error processing class.
Generally, you should not directly create a pear instance, but instead derive a new class and then create an instance of the new class. As a base class, pear provides us with some useful functions, mainly including destructor and error handling.
Php supports constructor, but does not support destructor. However, php provides the register_shutdown_function () function, which can call back the registered function before the script ends. Therefore, pear uses this feature, simulation of destructor is provided. If you have a child class of pear called mypear

Example content: What is pear? why is pear used? What benefits does pear bring to me? Pear encoding 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: 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.