在 PHP 函数末尾自动执行函数

发布于 2024-09-28 21:07:38 字数 216 浏览 0 评论 0原文

我希望在每个函数的末尾调用自定义错误日志/调试函数。

示例:

  • 我想调用 error_log(__METHOD__);
  • 我想 echo $query;
  • 显示执行时间等。

在每个函数的末尾用于调试目的,而不必每次都调用该自定义函数。

非常感谢。

i'm looking to call a custom error log/debug function at the end of each function.

example:

  • i want to call error_log(__METHOD__);
  • I want to echo $query;
  • show execution time etc..

at the end of every function for debug purposes without having to call that customized function every time.

much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-10-05 21:07:38

为此,请使用调试器/分析器,例如使用 XDebug 或 Zend 调试器。

有关两者的比较,请参阅

Use a Debugger/Profiler for this, e.g. use XDebug or Zend Debugger.

For a comparison of the two, see

野生奥特曼 2024-10-05 21:07:38

经过多次 RTM 之后,我不得不使用 debug_backtrace();,尽管它很昂贵。

我是这样做的:

// debug(debug_backtrace(),$query);
// $query is optional
define('DEBUG',true);

function debug($trace,$query = null) {
    if(DEBUG) {
        $caller=array_shift($trace);
        error_log("Initiating class: " . $caller['class']);
        error_log("Calling function: " . $caller['function']);
        error_log("In file: " . $caller['file']);
        error_log("@ line: " .$caller['line']);
        if(isset($query))
        {
            error_log("Performing Query: " .$query);
        }
        error_log("---");
    }
   else
       exit();
}

在每个函数的末尾我添加以下内容:

function init_userInfo($ai, $v) {
    $this->user[$ai] = $v;
    debug(debug_backtrace());
}

或者如果该函数有一个 SQL 查询:

function insertQuery($query)
{
    mysql_query($query)
        or die("MySQL Error: " . mysql_error());
    debug(debug_backtrace(),$query);

}

输出通常在 php_error.log 中如下所示:

[20-Oct-2010 19:02:07] Initiating class: Db
[20-Oct-2010 19:02:07] Calling function: selectQuery
[20-Oct-2010 19:02:07] In file: /Code/classes/user.class.php
[20-Oct-2010 19:02:07] @ line: 100
[20-Oct-2010 19:02:07] Performing Query: SELECT * FROM user WHERE uid=(3) LIMIT 1
[20-Oct-2010 19:02:07] ---
[20-Oct-2010 19:02:07] Initiating class: User
[20-Oct-2010 19:02:07] Calling function: htmlform_addUserInfo
[20-Oct-2010 19:02:07] In file: /Code/index.php
[20-Oct-2010 19:02:07] @ line: 6
[20-Oct-2010 19:02:07] ---

after much RTM i had to use debug_backtrace(); even though it's expensive.

here's how i did it:

// debug(debug_backtrace(),$query);
// $query is optional
define('DEBUG',true);

function debug($trace,$query = null) {
    if(DEBUG) {
        $caller=array_shift($trace);
        error_log("Initiating class: " . $caller['class']);
        error_log("Calling function: " . $caller['function']);
        error_log("In file: " . $caller['file']);
        error_log("@ line: " .$caller['line']);
        if(isset($query))
        {
            error_log("Performing Query: " .$query);
        }
        error_log("---");
    }
   else
       exit();
}

and at the end of each function i add the following:

function init_userInfo($ai, $v) {
    $this->user[$ai] = $v;
    debug(debug_backtrace());
}

or if the function has an SQL query:

function insertQuery($query)
{
    mysql_query($query)
        or die("MySQL Error: " . mysql_error());
    debug(debug_backtrace(),$query);

}

the output is generally like this in the php_error.log:

[20-Oct-2010 19:02:07] Initiating class: Db
[20-Oct-2010 19:02:07] Calling function: selectQuery
[20-Oct-2010 19:02:07] In file: /Code/classes/user.class.php
[20-Oct-2010 19:02:07] @ line: 100
[20-Oct-2010 19:02:07] Performing Query: SELECT * FROM user WHERE uid=(3) LIMIT 1
[20-Oct-2010 19:02:07] ---
[20-Oct-2010 19:02:07] Initiating class: User
[20-Oct-2010 19:02:07] Calling function: htmlform_addUserInfo
[20-Oct-2010 19:02:07] In file: /Code/index.php
[20-Oct-2010 19:02:07] @ line: 6
[20-Oct-2010 19:02:07] ---
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文