PEAR: Common Modules _php Tutorials

Source: Internet
Author: User
Tags crypt glob greatest common divisor pear xml parser

In the previous article, we introduced the concept of pear, coding rules, simple use methods, you may have a preliminary understanding of it. This time, we will describe the functionality of some of the modules in the existing Pear library and how it is used.
First, naming conventions
Before we get to know the existing pear modules, let's take a look at the organization classification and naming conventions of pear. The modules in the pear are organized in a similar way to CPAN, where each module's related files are placed under its own category directory, and others are placed directly under the root directory of the pear (single file). Since pear does not have a namespace like Java, your class name should be able to reflect the relationship between your module name or the parent class name, keeping certain conventions, such as your module name: "Mp3/common", then your PHP file should be located at: mp3/common.php, The class name of your module should be: Mp3_common. In general, if your module is based on one of the existing modules, it is recommended that you put your existing module under the same directory. If you are designing a new class and module, you can create a new directory yourself, or just put it under the same directory for similar purposes. For example, you have a new module for processing the log, it is recommended that you put it under the log/, indicating that it is used for log processing of the application module, if the new module is for processing MP3, then you can create a new directory MP3, placed under the MP3 directory.

Second, the existing Pear module
Since most of pear's modules are still in development, so here are the modules in the pear that are released along with php4.05, it should be noted that some abstract classes or base classes (such as mail.php,log.php,cache.php) are not listed. We are just focusing on modules with specific functions. Here is a list of these modules:

Benchmark/timer Test your PHP code to run efficiently
Benchmark/benchmark_iterate testing the performance of one of your functions during loop execution
Cache/output can cache the output of your PHP script in a variety of ways (file, database, or shared memory), and if you want to use this module to increase the load on the server, if you wish to provide efficiency through the caching of dynamic scripts, use Zend Optimize, this module may not be suitable for
Cache/graphics can cache images that you need to dynamically output
Console/getopt processing module for command line arguments
CMD a virtual shell that you can use to run some system commands
CRYPT/CBC realization of Perl CRYPT::CBC module simulation
CRYPT/HCEMD5 implementation of Perl CRYPT::HCE_MD5 module functions

DATE/CALC implementation date-related operations
Conversion of the Date/human Human calendar
DB provides a unified, abstract database operations layer with back-end support for multiple databases
File/find File Lookup
FILE/PASSWD manipulate password-class files, such as Password,httppass,cvspassword
File/searchreplace finding a replacement string in a file
Html/form can quickly create a Form in HTML
html/it implementation of template customization, the ability to dynamically generate pages, similar to the template function in Phplib, but to be easy to use
HTML/ITX extends your IT capabilities for more flexibility in customizing your templates for more complex operations
Html/processor xml_parser extension, which allows the operation of the HTML file to be applied
Http/compress a wrapper class for the PHP output buffering mechanism while compressing the buffered content
Image/remote no need to download the entire picture to the local to obtain the remote system picture information,
Log/composite Horde an extension to the log abstract class that enables multiple log processing objects to get the same log event. Note that the modules under the log directory are part of the Horde project, most of which are abstract superclass
Log/file writing log information to a file
Log/mcal sending information to a database in the local or remote schedule management software-mcal
A super class of observer in Log/observer Horder
Log/sql sending log information to an SQL database
Log/syslog sending information to the Syslog
mail/rfc822 Check if an email address is a valid rf822 email address
Mail/sendmail use SendMail to send letters
MAIL/SMTP using an SMTP server to send letters
Mathematical calculation of fractal math/fraction processing
Math/util Calculation Greatest Common divisor
Net/curl Object-oriented packaging for PHP's Curl extension
Net/dig manipulating Dig for DNS-related query operations
NET/SMTP implementing the SMTP protocol with Net/socket
Net/socket Universal Socket class, which implements the common socket operation wrapper
Numbers/roman the conversion of Arabic numerals and Roman numerals
Payment/verisign implementation and Verisign payment gateway interaction
Pear provides 2 basic classes of Pear modules, Pear and Pearerror classes
Pear/installer PEAR Installation class that provides similar functionality as CPAN modules in Perl
PHPDOC automatically generate API documentation from PHP code
Schedule/at interacting with the at Daemon on Unix
Xml/parser XML parser based on PHP XML extension
Xml/render generates an XML document in another format (html,pdf), which is an abstract class that already has HTML implementations in the latest Pear CVS code
XML/RPC uses PHP to implement an abstract class of XML-RPC that has been implemented xml/rpc/server in the latest pear CVS code

Three, the main module use introduction
Now we will briefly introduce some of the more commonly used, and the function has been relatively perfect and stable, can be used for "combat" module, which for a few powerful modules DB,PHPDOC,XML_PARSER,IT,ITX will be introduced in a future article separately.

1.pear/installer
This module belongs to the core module of pear itself, it completes the installation and maintenance of pear other modules, similar to the functions of CPAN module in Perl, but there is only install function, other such as query, check dependency and so on are not completed, pear itself is not similar cpan That kind of open site, but with the growing number of developers involved in pear, everything will be there.

Usage Syntax: Pear_installer::installer ($file)
$file is the module file that needs to be installed, either a local file or a remote file, such as http://or Ftp,installer, which is automatically downloaded locally. Files are generally packaged using gzip, which includes a package.xml file that describes the information about your module, such as the files involved, interdependencies, and, of course, the PHP files that are included in your module. The Pacakage.xml DTD file is under the Pear directory, and the name is 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
While most PHP applications rarely invoke system commands because they are web-based, they avoid invoking system commands directly from the operational efficiency and load considerations of the system, but in some special applications or when you are willing to use PHP as a shell tool, Invoking an existing system tool is unavoidable. CMD allows you to easily execute a series of system commands.

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

$options is a constant, which can be the following value:
Cmd_shutdown: Executing commands with the SHUTDOWN function
Cmd_shell: Specify the path of the SHELL
Cmd_output: Whether to mask the standard output of the command
Cmd_nohup: Execute command using NOHUP background
Cmd_verbose: Printing errors to standard output

Command ($command)
Add the command you want to execute, $command can be an array or a normal string

EXEC ()
Execute a command that has already been added

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 2 modules let you test how efficient your code is, and I think it's useful for system debugging: You can try out different algorithms, carefully examine the time each algorithm needs to run, and then choose the best way. Benchmark/timer the time difference in the test run at 2 different points in time, Benchmark/iterate is the duration of the timer extension, which is required to test the number of times a certain code (function) is run.

Use syntax: Benchmark/timer

Timer::setmarker ($name) Set the current point in time to $name
Timer::start () Start test
Timer::stop () Stop test
timer::timeelapsed ($start = start, $end = Stop) Calculates the time difference between the 2 points of $start and $end
Timer::getprofiling () returns the elapsed time 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 ()
Loops through the specified function. This is a method with variable parameters, the first parameter is the number of times to loop, the 2nd parameter is the function to execute, and the 3rd parameter is the argument to pass to the test function.

Iterate::get ()
Returns the time spent in the test

Require_once "benchmark/iterate.php";

$benchmark = new Benchmark_iterate;

function foo ($string)
{
Print $string. "
";
}
$benchmark->run (+, foo, test);
$result = $benchmark->get ();
?>

3.file/find
&glob ($pattern, $dirpath, $pattern _type=php)
Search for $pattern-compliant directories and files in $dirpath, return matching files and directories an array groups

&search ($pattern, $directory, $type =php)
Searches for files in $directory that conform to the $pattern rule, and returns an array of matching file names (note that only files, not including subdirectories). $pattern is the search condition you want to specify, usually a regular expression, $patten _type specifies what pattern to use for the rule expression, the default is PHP mode, you can also specify "Perl" to use the Perl pattern of the regular expression

Tip: Unlike search and glob, Glob does not recursively look up subdirectories, and search recursively searches 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 (). "";
}
/

http://www.bkjia.com/PHPjc/486622.html www.bkjia.com true http://www.bkjia.com/PHPjc/486622.html techarticle in the previous article, we introduced the concept of pear, coding rules, simple use methods, you may have a preliminary understanding of it. This time, we will introduce some of the modules in the existing Pear library ...

  • Related Article

    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.