#1 jake
在日常的编程开发当中,开发者经常会使用到对变量的调试,而sp框架提供的变量调试输出函数——dump正好满足了变量调试的需求。下面来介绍一下dump函数的使用方法。
dump —— 变量格式化输出函数
用法:
dump($vars, $output = TRUE, $show_trace = FALSE)
参数:
$vars,需要输出的变量,可以是普通变量,也可以是对象等。
$output,是否直接输出,如设置成FALSE,dump将返回输入内容而并不会将信息直接输出到浏览器上。
$show_trace,是否显示程序执行信息,如设置成TRUE,则将显示每一步的程序执行路径。
例子:
// 调试一个数组变量及调试lib_guestbook类的对象
$userObj = spClass('lib_guestbook');
$args = array(
'username' => 'jake'
);
dump($args); // 格式化输出数组$args
dump($userObj); // 格式化输出lib_guestbook类的对象$userObj
输出结果:
Array
(
[username] => jake
)
lib_guestbook Object
(
[pk] => gbid
[table] => guestbook
[addrules] => Array
(
[youare] => Array
(
[0] => lib_guestbook
[1] => youare
)
)
[verifier] => Array
(
[rules] => Array
(
[username] => Array
(
[notnull] => 1
[minlength] => 5
[maxlength] => 20
[equalto] => email
[youare] => jake
)
[email] => Array
(
[notnull] => 1
[email] => 1
[minlength] => 10
[maxlength] => 20
)
)
[messages] => Array
(
[username] => Array
(
[notnull] => 姓名不能为空
[minlength] => 姓名不能少于5个字符
[maxlength] => 姓名不能大于20个字符
[equalto] => 怎么不等于电邮呢
[youare] => 你不是jake
)
)
)
[linker] =>
[tbl_name:protected] => spcms_guestbook
[_db:private] => mysql Object
(
[conn:private] => Resource id 20
[arrSql] => Array
(
[0] => SET NAMES UTF8
[1] => SELECT spcms_article.* FROM spcms_article ORDER BY artid DESC LIMIT 5
[2] => SELECT spcms_article.* FROM spcms_article ORDER BY artid DESC LIMIT 5
[3] => SELECT spcms_article.* FROM spcms_article ORDER BY counter DESC LIMIT 8
[4] => SELECT spcms_article.* FROM spcms_article ORDER BY counter DESC LIMIT 8
[5] => SELECT spcms_acl.* FROM spcms_acl WHERE controller = 'main' AND action = 'download' limit 1
)
)
)
使用dump追踪程序执行过程中的变量取值,是最常见的测试和编程手段,所以希望大家能善用dump调试函数。在程序出现问题时多些使用dump来调试变量,可能会很轻易就能解决问题了。
2012-08-05 11:01:53