[置顶] Qse_XDebug -- Firefox 的PHP调试控制台

本文介绍了一种自制的PHP调试工具,该工具模仿了FirePHP的功能,可在Firefox浏览器中使用。文章详细展示了如何通过自定义类实现类似Firebug的日志记录功能,并提供了简单的使用示例。
写道
/**
* Firefox 的PHP调试控制台,firephp要求FF3.5+PHP5才能使用
* 很麻烦,这里自己实现一个类似的功能
* firebug 日志信息: http://getfirebug.com/logging
*/

 

class Qse_XDebug {
	
	private $met_out = array(
		'log' => 'log' ,
		'debug' => 'debug' ,
		'info' => 'info' ,
		'warn' => 'warn' ,
		'error' => 'error' ,
		'time' => 'time' ,
		'timeEnd' => 'timeEnd' ,
		'profile' => 'profile' ,
		'profileEnd' => 'profileEnd' ,
		'trace' => 'trace' ,
		'group' => 'group' ,
		'groupEnd' => 'groupEnd' ,
		'dir' => 'dir' ,
		'dirxml' => 'dirxml' ,
	); 
	
	protected $info_store = array();

	public function __construct(){
		$this->group(get_class($this) . " 调试控制台");
	}
	
	private function js_text($js_code=null){
		return "{$js_code}\n" ;
	}
	
	function __call($funcname,$args){
		if (!empty($funcname) && isset($this->met_out[$funcname]))
		{
			$object = array(
				$funcname => $args
			);
			$this->insert_data($object);
		}
	}
	
	private function insert_data($object=null){
		array_push($this->info_store,$object);
	}
	
	function execute($returnvalue=false){
		$output = "<script>%s</script>" ;
		$content = '' ;
		foreach ($this->info_store as $info){
			if (!is_array($info)) continue ;
			foreach ($info as $met=>$args){				
				$str_args = $this->parse_values($args) ;
				$content .= $this->js_text("console.{$met}({$str_args});");	
			}
		}
		if ($return_value)
			return sprintf($output,$content) ;
		echo sprintf($output,$content) ;
	}
	
		private function parse_values($args){
		if (!is_array($args)) return '' ;
		$values = $val = '' ;		
		foreach ($args as $arg){			
			if (is_string($arg))
				$val = "'{$arg}'";
			else if(is_numeric($arg))
				$val = $arg ;
			else if(is_array($arg) || is_object($arg)){
				if (function_exists('json_encode'))
					$val = json_encode($arg);	
			}
			$values .= $val . ',' ;
		}
		
		return preg_replace('/,$/','',$values) ;
	}
}

 demo:

<?php 
	$xdebug->log("hellow world!");
 $xdebug->group("组测试");
 $xdebug->error("测试 %s ; 时间: %d","旅游景点",time());
 $xdebug->warn("hellow world!",'chouzhutou',array(1,2,4,QContext::instance()));
?>

 

  • 大小: 8.6 KB
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值