php 调用方法 自动日志,PHP5中利用__call记录方法调用日志

还是在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";

}

?>

------------正 文 已 结 束, 感 谢 您 的 阅 读 (折雨的天空)--------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值