PEAR: common modules

Source: Internet
Author: User
Tags crypt glob

In the previous article, we introduced the PEAR concept, encoding rules, and simple usage. You may have a preliminary understanding of it. This time, we will introduce the functions and usage of some modules in the existing PEAR library.
I. Naming Conventions
Before learning about the existing pear module, let's take a look at the Organization classification and naming conventions of PEAR. The module organization in PEAR is similar to that in CPAN. The files related to each module are placed under their own classification directories, while others are directly placed under the pear root directory (a single file ). Because PEAR does not have a namespace like java, your class name should be able to reflect the relationship between your module name or parent class name. It complies with certain conventions, for example, your module name: "Mp3/common", so your php file should be located at: Mp3/common. php. The class name of your module should be Mp3_common. Generally, if your module is improved based on an existing module, we recommend that you put your module and the existing one under the same directory. If you are designing a new class and module, you can create a new directory by yourself or put it under the same directory for similar purposes. For example, if you write a new module for processing logs, we recommend that you put it under Log/to indicate that it is an application module for Log processing; if the new module is used to process mp3 files, you can create a new mp3 file directory and put it under the mp3 file directory.

Ii. Existing PEAR Module
Most of the Pear modules are still under development. Therefore, this section lists the modules in pear released along with php4.05. Note that some abstract classes or base classes (such as Mail. php, Log. php, Cache. php) is not listed. We only focus on modules with specific functions. The following is a list of these modules:

Benchmark/Timer test the efficiency of a piece of php code
Benchmark/Benchmark_Iterate test the performance of a function during loop execution
Cache/Output can Cache the Output of your php script, and can be cached in multiple ways (in files, databases, or shared memory ), if you use this module to increase the server load, you may wish to use Zend optimize If You Want To provide efficiency through the dynamic script cache. This module may not be suitable
Cache/Graphics can Cache the images you need to dynamically output
Console/Getopt command line parameter Processing Module
CMD a virtual shell that can be used to run some system commands
Simulation of Crypt/CBC to implement Perl Crypt: CBC Module
Crypt/HCEMD5 implement the Perl Crypt: HCE_MD5 module function

Date/Calc Date-related operations
Conversion of Date/Human calendar
DB provides a unified and abstract database operation layer, and supports multiple databases at the backend.
File/Find File search
File/Passwd files that manipulate the password class, such as password, httppass, cvspassword
File/SearchReplace search for replacement strings in the File
HTML/Form: You can quickly create a form in html.
HTML/IT allows you to customize templates and dynamically generate pages. Similar to the template function in phplib, IT is easy to use.
HTML/ITX provides IT extension functions, allowing you to flexibly customize your templates for more complex operations.
Extension of HTML/Processor XML_Parser, which can be applied to operations on html files
HTTP/Compress is a packaging class used for the Php output buffer mechanism. It can also Compress and store the buffered content.
Image/Remote can obtain the Image information of the Remote system without downloading the entire Image to the local device,
Log/composite Horde extends the log abstract class so that multiple Log processing objects can obtain the same log event. Note that the modules under the Log directory are part of the Horde project, and most of them are abstract superclasses.
Log/file write Log information to the file
Log/mcal sends information to the local or remote calendar management software-mcal Database
A superclass of observer in Log/Observer Horder
Log/SQL sends Log information to the SQL database
Log/syslog sends information to syslog
Mail/RFC822 check whether an email address is a valid rf822 email address
Mail/sendmail use sendmail to send Mail
Mail/smtp use smtp server to send Mail
Math/Fraction processing the mathematical computation of Fragment
Math/Util
NET/Curl: Object-oriented packaging of php Curl extensions
NET/Dig to manipulate dig and perform dns-related query operations
NET/SMTP use NET/Socket to implement SMTP protocol
NET/Socket generic Socket class, which implements packaging of common socket operations
Conversions between Numbers/Roman Arabic numerals and Roman numerals
Interaction between Payment/Verisign and Verisign Payment gateway
Pear provides two basic classes for the Pear module: PEAR and PEARError.
The installation class of PEAR/Installer pear, which provides functions similar to the CPAN module in Perl
PHPDoc automatically generates API documentation from php code
Interaction between Schedule/at and AT daemon on Unix
XML/Parser xml Parser based on php xml Extension
XML/Render generates other formats (html, pdf) for the xml document. This is just an abstract class and has been implemented in the latest pear cvs code.
XML/RPC: an abstract class for implementing xml-rpc using php. The xml/RPC/Server implementation is available in the latest pear cvs code.

Iii. Introduction to main modules
Now we will briefly introduce some commonly used modules with relatively complete and stable functions. They can be used in "practice" modules. For some of the powerful modules Db, phpdoc, XML_Parser, IT and ITX will be introduced separately in future articles.

1. PEAR/Installer
This module is the core module of pear. It completes the installation and maintenance of other pear modules, similar to the functions of the cpan module in perl. However, currently only the install function is available, and other functions such as query, check dependencies and so on are not completed, pear itself is not as open as cpan site, but with the increasing number of pear developers, everything will be there.

Syntax: PEAR_Installer: installer ($ file)
$ File is a module file to be installed. It can be a local file or a remote file, such as http: // or ftp. The installer automatically downloads the file to the local device. Files are generally packaged using gzip, including a package. xml file, used to describe the information about your module, such as the contained files and mutual dependencies. In addition, you must include the PHP file of your module. The DTD file of pacakage. xml is under the pear directory and named package. dtd.

<? Php
Require_once "PEAR/Installer. php ";
$ Installer = new PEAR_Installer;
// Install the specified Module
$ Result = $ installer-> install ($ package_file );
If (PEAR: isError ($ result )){
Echo "Install $ package_file failed! ";
} Else {
Echo "Install $ package_file sucess! ";
}
?>
2. CMD
Although most php applications seldom call system commands, because these applications are web-based, you must avoid directly calling system commands in terms of running efficiency and system load. However, in some special applications or when you are willing to use php as a shell tool, it is inevitable to call the existing system tool. CMD allows you to easily execute a series of system commands.

Syntax: setOption ($ option, $ setting)
Set the parameter $ options to $ setting.

$ Options is a constant, which can be the following values:
__Shutdown: Execute the command through the shutdown Function
CMD_SHELL: Specifies the shell path.
Performance_output: whether to block standard output of commands
CMD_NOHUP: Use the nohup background to execute the command
Pai_verbose: prints errors to standard output

Command ($ command)
Add the command to be executed. $ command can be an array or a common string.

Exec ()
Run the added command

<? Php
Require_once "CMD. php ";
$ Cmd = new CMD;
$ Cmd-> command (tar zcvf test.tar.gz ~ /Test );
If ($ cmd-> exec ()){
Echo "success! ";
} Esle {
Echo "Error:". $ cmd-> lastError;
}
?>

3. Benchmark/Timer and Benchmark/Iterate
These two modules allow you to test the running efficiency of your code. I think this is useful for system debugging: You can try different algorithms, carefully examine the time required for each algorithm, and then select the best method. The time difference between the Benchmark and Timer tests at two different time points. The Benchmark and Iterate tests the time required to run a certain code (function) n times.

Syntax: Benchmark/Timer

Timer: setMarker ($ name) sets the current time point to $ name
Timer: start () start test
Timer: stop () stop Test
Timer: timeElapsed ($ start = Start, $ end = Stop) Calculate the time difference between $ start and $ end.
Timer: getProfiling () returns the time consumed between start and stop.
<? Php
Require_once "Benchmark/Timer. php ";
$ Timer = new Benchmark_Timer;

$ Timer-> start ();
$ Timer-> setMarker (Marker 1 );
$ Timer-> stop ();

$ Profiling = $ timer-> getProfiling ();
?>

Benchmark/Iterate

Iterate: run ()
Runs the specified function cyclically. This is a method with variable parameters. The first parameter is the number of cycles, 2nd parameters are the function to be executed, and 3rd parameters are the parameters to be passed to the test function.

Iterate: get ()
Returns the test time.

<? Php
Require_once "Benchmark/Iterate. php ";

$ Benchmark = new Benchmark_Iterate;

Function foo ($ string)
{
Print $ string ."
";
}
$ Benchmark-> run (100, foo, test );
$ Result = $ benchmark-> get ();
?>

3. File/Find
& Glob ($ pattern, $ dirpath, $ pattern_type = php)
Search for directories and files that match $ pattern in $ dirpath, and return an array of matched files and directories.

& Search ($ pattern, $ directory, $ type = php)
Search for files that comply with the $ pattern Rule in $ directory and return an array of matched file names (Note: Only files, excluding subdirectories ). $ Pattern is the search condition to be specified. It is generally a rule expression. $ patten_type specifies the rule expression of the mode used. The default value is the php mode, you can also specify "perl" to use the regular expression in the perl mode.

Note: The difference between search and glob is that glob does not Recursively search for subdirectories, while search does Recursively search for subdirectories.

<? Php
Require_once "File/Find. php ";
$ Find = new File_Find;
// Search the current directory
$ Php_files = $ find-> glob ("* php ",".");
If (PEAR: isError ($ php_files )){
Die "error:". $ php_files-> getMessage ()."";
}
/

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.