PHP - 识别函数何时被调用

发布于 2024-08-18 08:10:50 字数 188 浏览 6 评论 0原文

我正在考虑如何找到调用任何函数的位置。问题是我需要找到 PHP 调用 mail() 函数的位置。一种方法是使用 register_tick_function(),但我需要打开每个文件并检查每行上的内容。该项目非常庞大,用 PHP 解析每个文件需要很长时间。还有其他办法吗?或者选择如何覆盖 mail() 函数?

I'm thinking about how to find from where any function was called. The problem is that I need to find where the PHP is calling mail() function. One way will be to use register_tick_function(), but I'll need to open each file and check what is on each line. The project is huge, it will take really long to parse each file in PHP. Any other way? Or option how to override the mail() function?

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

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

发布评论

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

评论(5

挽你眉间 2024-08-25 08:10:50

要覆盖内置邮件功能,请查看 override_function这是高级 PHP 调试器 PECL 扩展的一部分 - 那么你可以使用 debug_backtrace 找出调用者详细信息...

//define code to override mail function (note I've used php5.3 nowdoc syntax to avoid 
//the need to escape the dollar symbols!!
$code=<<<'CODE'
    $trace=debug_backtrace();
    $caller=array_shift($trace);

    echo 'mail() called by '.$caller['function']
    if (isset($caller['class']))
        echo 'in '.$caller['class'];
CODE;

//install override
override_function('mail', '$to,$subject,$msg,$hdrs,$params', $code);

To override the built-in mail function, take a look at override_function which is part of the Advanced PHP Debugger PECL extension - then you can use debug_backtrace to find out the caller details...

//define code to override mail function (note I've used php5.3 nowdoc syntax to avoid 
//the need to escape the dollar symbols!!
$code=<<<'CODE'
    $trace=debug_backtrace();
    $caller=array_shift($trace);

    echo 'mail() called by '.$caller['function']
    if (isset($caller['class']))
        echo 'in '.$caller['class'];
CODE;

//install override
override_function('mail', '$to,$subject,$msg,$hdrs,$params', $code);
挽心 2024-08-25 08:10:50

您可以使用 debug_backtrace()。这将包含有关调用方法/函数等的信息。请参阅手册中的示例。

要向现有函数添加行为,请将函数包装到您自己的函数或类中,然后调用它而不是本机函数。

要完全重新定义本机函数,您必须安装 runkit< /代码>。然后你可以做 runkit_redefine_function() (或按照其他地方的建议使用 APD)。

如果您只想知道在项目中的何处调用了 mail(),例如您不需要在运行时对其进行评估,请使用 IDE 的搜索功能。 Eclipse、Zend Studio 和 Netbeans 可以进行文件搜索,因此应该很容易找到调用并替换它们。

You can inspect the stack trace with debug_backtrace(). This will contain information about the calling method/function among others. See the manual for examples.

To add behavior to an existing function, wrap the function into your own function or class and then call this instead of the native function.

To completely redefine a native function, you'd have to install runkit. Then you could do runkit_redefine_function() (or use APD as suggested elsewhere).

If you just want to know where in your project mail() was called, e.g. you do not need to evaluate this at runtime, use your IDE's search function. Eclipse, Zend Studio and Netbeans can do file searches, so it should be very easy to find the calls and also to replace them.

昔梦 2024-08-25 08:10:50

强力方法是在代码中进行全局搜索和替换,将“mail\s(”替换为“my_mail(”),然后定义 my_mail 并将您想要的任何日志记录功能放在那里。

The brute force approach would be to do a global search and replace in your code, replacing "mail\s(" with "my_mail(", then define my_mail and put whatever logging functionality you want there.

嘿咻 2024-08-25 08:10:50

为什么不简单地搜索“mail(”的源代码?

Why don't you simply search the source for "mail("?

小兔几 2024-08-25 08:10:50

我想你可以访问源代码吗?

为什么不直接使用像 jEdit 这样的编辑器,并在所有打开的缓冲区中查找所有出现的 mail(* ?

或者您真的需要在运行时知道行号?我无法想象您实际上会这样做。

I take it you have access to the source code?

Why not just use an editor like jEdit, and find all occurences of mail(* in all open buffers?

Or do you really need to know the line numbers at runtime? I can't imagine that you actually do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文