Home php教程 php手册 初识PEAR

初识PEAR

Jun 21, 2016 am 09:12 AM
gt nbsp pear php

仙人掌工作室  

一、什么是PEAR?为什么需要它?  

为了创建一个类似于Perl CPAN档案的工具,Stig S. Bakken创立了PEAR项目。PEAR的基本目标是发展成为PHP扩展和库代码的知识库,而这个项目最有雄心的目标则是试图定义一种标准,这种标准将帮助开发者编写可移植、可重用的代码。  

Internet上已经有关于该项目的一些文档。例如,一些初始的资料已经可以在PHP正式手册中找到,更多的资料即将加入。  

在很大程度上,PEAR还是一项正在进行之中的工作。在未来的几个月内,PEAR安装程序和PEAR网站将有很大的发展。为了在下一个PHP版本中使用PEAR管理数量日益增长的C扩展,以及用PEAR安装程序作为用户把扩展下载和安装到PHP的前端工具,在PHP Core Developer邮件列表上已经有了大量的讨论。  

另外,我们还必须关注PEAR的PHP库代码。PEAR的PHP库代码常常被认为是当前设计最好、最整洁的库。PEAR::DB是其中最受欢迎的一个包,这个包是PEAR项目创建的一个数据库抽象库。Bakken正在努力,要把它主要的类和函数移植到C扩展上,使得这个包具有最好的性能。  

毫无疑问,这个新的项目必将成为未来PHP最重要的组成部分之一,它使得开发者能够方便地通过PEAR网站共享代码,使得用户能够方便地下载和安装扩展、PHP代码库。  

二、如何使用PEAR?  

正如前面提到的,PEAR正在不断地发展和改进。不过,现在已经有许多包可供使用。其中最受欢迎的一个包就是PEAR::DB,它使得开发者能够编写出可同时用于多种不同数据库服务器的代码。例如,开发者能够编写出把数据库记录插入表的脚本,而且这个脚本适用于MySQL、PostgreSQL和Oracle。  

也就是说,下面这个实例能够适用于所有不同类型的数据库服务器:  

// 引入合适的PEAR类
require_once("DB.php");

$dsn = array(
'phptype'  => 'mysql',
'hostspec' => 'localhost',
'database' => 'test_db',
'username' => 'test_user',
'password' => 'test_password'
);
$dbh = DB::connect($dsn);

$stmt = "SELECT id, name FROM examples ORDER BY id";
$result = $dbh->simpleQuery($stmt, DB_FETCHMODE_ASSOC);
if ($dbh->numRows($result) > 0) {
$data = (object) $dbh->fetchRow($result, DB_FETCHMODE_ASSOC);
echo "id   => $data->id
\n";
echo "name => $data->name
\n";
}
?>


这是一个简单的例子,显示了使用象PEAR::DB之类的抽象库编程究竟是什么样子。  

最近,PHP基本手册中加入了一章新的内容。但是,究竟是由该手册包含整个PEAR库的说明,还是为各个PEAR包编制独立的手册,这一切尚未明了。  

三、安装  

安装PEAR实际上很简单。由于PEAR库更新很快,所以下面我要介绍的是如何安装PEAR最新的CVS版本。  

注意:PEAR内部知识库系统将来可能要发生变化。就目前来说,PEAR包、库与PHP本身一起保存到同一CVS树,它可能导致在管理PHP主知识库和开发者、PEAR本身的知识库和开发者这两方面都出现问题。可以预料,在不远的将来,PEAR将分离成为独立的模块/树。这个模块已经存在,但大部分的包仍旧在老位置。  

如果你从来没有听说过CVS,请看看相关资料。CVS不是一种特别难用的技术,但熟悉它却需要一定的时间。  

假设你的机器上已经安装好了标准的CVS客户软件,接下来你应该:  


打开一个终端窗口(rxvt,xterm,或其他)。  
输入下面的命令:  

cvs -z3 -d :pserver:cvsread@cvs.php.net:/repository login
Password: [在这里输入“phpfi”作为密码]
cvs -z3 -d :pserver:cvsread@cvs.php.net:/repository co php4
[这个命令将创建新的目录php4]
cd php4
cvs -z3 -d :pserver:cvsread@cvs.zend.com:/repository login
Password: [这里输入密码zend]
cvs -z3 -d :pserver:cvsread@cvs.zend.com:/repository co Zend TSRM
[该命令将在php4下面创建两个新目录,名字为“Zend”和“TSRM”]
./buildcon


这就是全部的安装过程。运行buildconf将创建标准的configure脚本,这个脚本可以用来设置PHP选项。在默认安装中,PEAR将自动安装到/usr/local/lib/php下。但是,你也可以手工构造各个文件,方法是:以超级用户身份进入,转到php4/pear目录,执行make install-su。  

除此之外,你还应该把PEAR根目录加入到php.ini文件的include_path。默认的目录应该是/usr/local/lib/php,但如果你手工修改了选项,它可能在其他位置。  

四、获取帮助  

PEAR的包涵盖了从数据库抽象层到输出缓冲系统的方方面面,因此PEAR远远不止是一个简单的库——它是一种编写整洁、可移植代码的新标准。对于PEAR新手来说,最重要的事情之一就是阅读PHP手册中出版的编码标准。下面是几个获得PEAR帮助信息的地方:  


查找PEAR-DEV或PEAR-GENERAL,寻求问题的答案。  
查阅随同PEAR源代码提供的单元测试脚本。这些脚本的位置在PHP CVS源代码所在目录之下。例如,在我这里,它是/home/jpm/php4/pear/DB/tests/。  
通读Tomas Cox优秀的PEAR::DB指南。  
到目前为止,PEAR的真面目尚未完全显露在世人眼前。但是,我希望本文已经成功地为你勾勒了它的大致面貌。  

好好享受吧!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1254
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles