什么是PEAR及其作用
什么是PEAR?
PEAR全称为PHP扩展与应用库(the PHP Extension and Application Repository)。它充当PHP代码的资源仓库,其地位类似于Perl生态中的CPAN(Comprehensive Perl Archive Network)。
作为PHP代码仓库,PEAR提供了大量实用代码片段,有助于开发者避免重复造轮子。需要注意的是,这些代码包并非直接通过HTTP网站下载,而是需要先完成PEAR环境的安装。
如何获取并安装PEAR
建议:以下操作基于Windows 7环境演示,同样适用于Windows XP、2003、Vista及2008系统。部分步骤具有尝试性质,为避免走弯路,建议先通读全文再进行实际操作。
若PHP目录中未自带PEAR组件(即找不到go-pear.php文件),则需手动下载该文件。点击下载链接并将文件另存为go-pear.php,放置于php.exe所在的PHP安装目录下。例如安装目录为D:\php5,则文件路径应为D:\php5\go-pear.php。
打开“开始”菜单,选择“运行”,输入cmd启动命令提示符。输入以下命令切换至go-pear.php所在目录,并执行该PHP脚本文件:
cd D:\php5 D: php go-pear.php 遗憾的是,由于PHP版本较新,上述操作可能提示失败,具体报错信息如下:
Deprecated: Assigning the return value of new by reference is deprecated in D:\php5\go-pear.php on line 733
Sorry! Your PHP version is too new (5.3.8) for this go-pear.
Instead use http://pear.php.net/go-pear.phar for a more stable and current version of go-pear, more suited to your PHP version.
Thank you for your coopertion and sorry for the inconvenience!
提示信息要求前往http://pear.php.net/go-pear.phar下载go-pear.phar文件,替换原有的go-pear.php后重试。下载好go-pear.phar文件并存入D:\php5文件夹,执行以下命令:
cd D:\php5 D: php go-pear.phar 此时终端出现如下提示:
Are you installing a system-wide PEAR or a local copy? (system|local) [system] : 按回车键默认选择system并继续。系统将显示默认的PEAR临时、数据、配置、测试及执行目录设置:
Below is a suggested file layout for your new PEAR installation. To
change individual locations, type the number in front of the
directory. Type 'all' to change all of them or simply press Enter to
accept these locations.
1. Installation base ($prefix) : D:\php5
2. Temporary directory for processing : D:\php5\tmp
3. Temporary directory for downloads : D:\php5\tmp
4. Binaries directory : D:\php5
5. PHP code directory ($php_dir) : D:\php5\pear
6. Documentation directory : D:\php5\docs
7. Data directory : D:\php5\data
8. User-modifiable configuration files directory : D:\php5\cfg
9. Public Web Files directory : D:\php5\www
10. Tests directory : D:\php5\tests
11. Name of configuration file : C:\Windows\pear.ini
12. Path to CLI php.exe : D:\php5
1-12, 'all' or Enter to continue:
直接回车使用默认值以继续安装。本以为一切顺利,但在此处却出现了错误:
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘UTC' for '8.0/no DST' instead in phar://D:/php5/go-pear.phar/PEAR/Registry.php on line 1012 PEAR_Config::writeConfigFile fopen(‘C:\Windows\pear.ini','w') failed (fopen(C:\Windows\pear.ini): failed to open stream: Permission denied)
错误表明没有权限写入C:\Windows\pear.ini。由于使用的是Windows 7系统且以普通用户身份打开命令提示符,因此无权写入C:\Windows目录。看来需要另辟蹊径。回顾之前的提示“Are you installing a system-wide PEAR or a local copy?”,网上多建议直接回车选system,但若不想改写C:\Windows目录,可选择local。于是重新执行php go-pear.phar,并选择local模式。
Are you installing a system-wide PEAR or a local copy? (system|local) [system] : local Please confirm local copy by typing 'yes' : yes
选择local后,安装过程恢复正常:-)
Would you like to alter php.ini ? [Y/n] : 接下来会出现上述提示,只有选择Y同意修改php.ini才能继续。
php.ini include_path updated.
Current include path : .;C:\php\pear
Configured directory : D:\php5\pear
Currently used php.ini (guess) : D:\php5\php.ini
Press Enter to continue:
随后出现配置确认界面,由于无需特殊更改,直接回车确认即可。
至此,屏幕上应显示“Thanks for using go-pear!”,恭喜PEAR安装成功!
如何安装PEAR Package包
接下来访问PEAR网站查看可用资源。每个PEAR包通常都附有安装命令提示,例如MDB2包提示使用pear install MDB2命令。假设目录仍为D:\php5,可使用以下命令尝试安装:
cd D:\php5 D: pear install MDB2 结果出现如下错误:
D:\php5>pear install MDB2
downloading MDB2-2.4.1.tgz ...
Starting to download MDB2-2.4.1.tgz (119,790 bytes)
..........................done: 119,790 bytes
ERROR: failed to mkdir C:\php\pear\docs\MDB2\docs\examples
无法创建目录,看来又是权限问题。索性以管理员身份启动cmd命令提示符,然后重新执行上述命令。这次总算安装成功,显示install ok!
另一个安装报错案例:PHP pear安装出现 Warning: require_once(Structures/Graph.php)...错误
在WINDOWS上安装PEAR一路顺利,接着想安装email包体验一下,但随即报错:
Warning: require_once(Structures/Graph.php): failed to open stream: No such file or directory in PEAR\Downloader.php on line 1217 PHP Warning: require_once(Structures/Graph.php): failed to open stream: No such file or directory in E:\Program Files\php5_3\pear\pear\PEAR\Downloader.php on l ine 1217 新手遇到此问题,上网寻找解决方案。折腾许久仍报此错。原因明确:缺少Structures_Graph这个PEAR应用。试图直接使用pear命令在线安装: pear install Structures_Graph-1.1.1 但令人沮丧的是,竟然也报同样的错误。
不再纠结,直接到PEAR官网下载Structures_Graph-1.1.0.tgz包。解压查看内部结构,发现其中包含Structures/Graph.php文件。于是将压缩包内的Structures文件夹复制到PEAR根目录下。接着再次运行Structures_Graph的安装命令,惊喜地发现成功了。
随后再安装email包,不出意外也成功。至此,这个困扰半天的问题终于解决。
谢谢大家的阅读哦!