折雨的天空
PHP5中利用__call记录方法调用日志
2011-8-23 我好笨

还是在PHP实战书上看到的,很多不懂,太高深了



看代码吧,自己可以拿去运行,log.php是Pear 的log类,自己去下载回来,在包的根目录里有



<?php
//PHP重载,利用pear log类 完成方法调用日志记录
require_once "./Log.php";

class Logging{
function __call($method,$args)
{
$method="_$method";
if(!method_exists($this,$method))
{
throw new Exception("Call to undefined method ".get_class($this)."::$method");
}
$log = Log::singleton('file',"./user.log",'Methods',NULL,LOG_INFO);
$log->log("Just starting method $method");
$return=call_user_func_array(array($this,$method),$args);
$log->log("Just finished method $method");
return $return;
}
}

class DateAndTime extends Logging
{
private $timestamp;

function __construct($timestamp=false)
{
$this->_init($timestamp);
}
protected function _init($timestamp)
{
$this->timestamp=$timestamp?$timestamp:time();
}
function getTimestamp()
{
return $this->timestamp;
}
protected function _before(DateAndTime $other)
{
return $this->timestamp < $other->getTimestamp();
}
}

$now=new DateAndTime();
$nexthour=new DateAndTime(time()+3600);
print_r(array($now,$nexthour));
if($now->before($nexthour))
{
echo "OK\n";
}
?>

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容