PEAR: common modules. 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 some of the existing models in the PEAR Library.
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.
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
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.
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.
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.
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 ()."";
}
/
Bytes. This time, we will introduce some existing models in the PEAR library...