<!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--END RESERVED FOR FUTURE USE INCLUDE FILES--> 虽然您可以使用 PHP 为系统管理和传统数据处理之类的任务创建命令行脚本,但是编程语言对 Web 应用程序的性能有主要影响。在使用过程中,每个 PHP 应用程序都驻留在服务器上,并且将通过代理(例如 Apache)调用 PHP 应用程序处理到来的请求。对于每个请求,典型的 PHP Web 应用程序在简短运行后将得到一个 Web 页面或 XML 数据结构。
假定经过简单的运行后,一个分层构造的 Web 应用程序 —— 包括客户机、网络、HTTP 服务器、应用程序代码和底层数据库 —— 将会很难隔离 PHP 代码中的错误。即使假定除了 PHP 代码以外所有层都可以正常运行,跟踪 PHP 代码中的错误也会非常难,尤其是在应用程序利用较多的类时更是如此。
PHP 语句 echo 和函数 var_dump()、debug_zval_dump() 和 print_r() 都是常见且流行的调试辅助工具,可以帮助解决多种问题。但是,这些语句 —— 甚至更健壮的工具,例如 PEAR Log package —— 都是取证工具,必须在上下文环境之外先进行推测分析才能生成证据。
在某种程度上,通过推论进行调试是一种蛮干的做法。收集并筛选数据,尝试推论出发生的问题。如果缺少重要信息,则必须重新测试代码、重复执行步骤,然后重新开始研究。一种更加高效的方法是在 程序运行时探测应用程序。您可以对请求参数分类,筛选过程调用堆栈,并查询任何所需的变量或对象。您可以暂时中断应用程序并且可以在变量更改值时收到警报。在某些情况下,您可以通过交互式询问 “如果……会怎样?” 问题来实际影响变量。
称为调试器 的特殊应用程序支持这种 “实时的” 或交互式的检查。调试器可能启动并连接到进程上以便控制进程并监测其内存。或者,在使用解释语言的情况下,调试器可以直接解释代码。典型的现代图形化调试器可以索引并浏览代码,以符合人类阅读习惯的形式轻松地显示复杂的数据结构,并同时显示程序状态,如调用堆栈、中间输出和所有变量的值。例如,调试器通常都会把类的属性和方法分类并进行描述。
在本文和下一篇文章中,我将介绍的工具一定能够简化 PHP 调试。下一次,我将主要介绍交互式调试和 Zend Debugger —— 一个特别针对 PHP 的健壮调试器 —— 并探究它提供的许多功能。(Zend Debugger 是一款商业产品,是 Zend PHP 集成开发环境(IDE)的一部分)。我还将介绍一款开源 PHP 调试器,以免您只愿把钱花在啤酒上,而不是花在代码上。但是,本文将主要介绍如何更好地取证。
类似《犯罪现场调查》,只是更令人讨厌
代码出错、未能生成某个所需结果或者彻底崩溃时,您需要回答四个 w 问题:where、what、why 和 when:
$ tar xzf xdebug-2.0.0RC4.tgz
$ cd xdebug-2.0.0RC4
运行 phpize 以准备适用于您的 PHP 版本的 Xdebug 代码:
$ phpize
Configuring for:
PHP Api Version: 20020918
Zend Module Api No: 20020429
Zend Extension Api No: 20021010
phpize 的输出是一个脚本 —— 通常名为配置 —— 用于调整其余的构建过程。
运行配置脚本:
$ ./configure
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
...
checking whether stripping libraries is possible... yes
appending configuration tag "F77" to libtool
configure: creating ./config.status
config.status: creating config.h
通过运行 make 构建 Xdebug 扩展:
$ make
/bin/sh /home/strike/tmp/xdebug-2.0.0RC4/libtool
--mode=compile gcc -I.
-I/home/strike/tmp/xdebug-2.0.0RC4 -DPHP_ATOM_INC
-I/home/strike/tmp/xdebug-2.0.0RC4/include
-I/home/strike/tmp/xdebug-2.0.0RC4/main
-I/home/strike/tmp/xdebug-2.0.0RC4
-I/usr/include/php4 -I/usr/include/php4/main
-I/usr/include/php4/Zend -I/usr/include/php4/TSRM
-DHAVE_CONFIG_H -g -O0 -c
/home/strike/tmp/xdebug-2.0.0RC4/xdebug.c -o
xdebug.lo mkdir .libs
...
Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).
使用 make 将生成 Xdebug 扩展 xdebug.so。
安装该扩展:
$ sudo make install
Installing shared extensions: /usr/lib/php4/20020429/
继续之前,使用鼠标选择并复制上一条命令显示的目录。该路径对于最后一步配置扩展至关重要。
在您喜欢的文本编辑器中打开 php.ini 文件,然后添加以下代码:
zend_extension = /usr/lib/php4/20020429/xdebug.so
xdebug.profiler_enable = Off
xdebug.default_enable = On
<?php
function deep_end( $count ) {
// add one to the frame count
$count += 1;
if ( $count < 48 ) {
deep_end( $count );
}
else {
trigger_error( "going off the deep end!" );
}
}
// main() is called to start the program,
// so the call stack begins with one frame
deep_end( 1 );
?>
<?php
class Person {
var $name;
var $surname;
var $age;
var $children = array();
function Person( $name, $surname, $age, $children = null) {
$this->name = $name;
$this->surname = $surname;
$this->age = $age;
foreach ( $children as $child ) {
$this->children[] = $child;
}
}
}
$boy = new Person( 'Joe', 'Smith', 4 );
$girl = new Person( 'Jane', 'Smith', 6 );
$mom = new Person( 'Mary', 'Smith', 34, array( $boy, $girl ) );
var_dump( $boy, $mom );
?>
清单 7 显示了 var_dump() 的输出。
清单 7. var_dump() 输出
object(person)
var 'name' => string 'Joe' (length=3)
var 'surname' => string 'Smith' (length=5)
var 'age' => int 4
var 'children' =>
array
empty
object(person)
var 'name' => string 'Mary' (length=4)
var 'surname' => string 'Smith' (length=5)
var 'age' => int 34
var 'children' =>
array
0 =>
object(person)
var 'name' => string 'Joe' (length=3)
var 'surname' => string 'Smith' (length=5)
var 'age' => int 4
var 'children' =>
array
empty
1 =>
object(person)
var 'name' => string 'Jane' (length=4)
var 'surname' => string 'Smith' (length=5)
var 'age' => int 6
var 'children' =>
array
empty