3

Obtain the file name and line number from the start function statement

 3 years ago
source link: https://www.codesd.com/item/obtain-the-file-name-and-line-number-from-the-start-function-statement.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Obtain the file name and line number from the start function statement

advertisements

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

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. }

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

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?


You're looking for 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK