获取函数声明开始的文件名和行号 [英] Get filename and line number of start of function declaration

查看:199
本文介绍了获取函数声明开始的文件名和行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

如何找出函数的定义?

包含php文件以知道它的文件名?

Possible Duplicate:
How to find out where a function is defined?
included php file to know it's file name?

我可以在PHP中获取函数声明开头的文件名行号吗?

Can I get the filename and line number of the start of a function declaration in PHP?

说,我有以下代码:

Say, I have the following code:

 1. /**
 2.  * This function shows the foo bar.
 3.  */
 4. function foo_bar($subject) {
 5.     echo "Foo bar:\n";
 6.     if ($subject == "none") {
 7.         trigger_error("Wrong argument given to 'foo_bar()' in ... on line ....", E_USER_WARNING);
 8.     }
 9.     ...
10. }

当我调用 foo_bar(none)时,函数必须抛出如下错误:

When I call foo_bar("none"), the function must throw an error like this:


警告:在第4行的/ home / web / mcemperor中给'foo_bar()'赋予错误的参数。

Warning: Wrong argument given to 'foo_bar()' in /home/web/mcemperor on line 4.

可以检索函数声明开始的文件名行号吗?

Can I retrieve the filename and the line number of start of the function declaration?

推荐答案

您正在寻找 ReflectionFunction

$r = new ReflectionFunction('foo_bar');
$file = $r->getFileName();
$startLine = $r->getStartLine();

就是这么简单......它适用于任何已定义的函数,无论您传递给构造函数参数。

It's that simple... It works for any defined function, whatever one that you passed in to the constructor argument.

这篇关于获取函数声明开始的文件名和行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆