Re: Getting the name of a function
| From: | Jochem Maas | Date: | Thu, 28 Feb 2008 10:06:17 +0000 |
| Subject: | Re: Getting the name of a function | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-270525@lists.php.net to get a copy of this message | ||
Daniel Brown schreef:
On Wed, Feb 27, 2008 at 1:56 PM, Richard S. Crawford <rscrawford@mossroot.com> wrote:huh? what about debug_backtrace() ... contains plenty of info, including function names of everything in the stack. of course something like xdebug can take it to a whole new level.For my own amusement, I'm writing a function that will print out detailed error messages for an API that I'm creating for a minor project. One of the pieces of information I'd like to return would be the name of the function that called the error function.[snip!]I know that I could pass the name of the function as a parameter to the error() function (e.g. error("bad_function","This is dumb")) but I'd rather keep it simpler than that. Is there a way to do this?Not without a lower-level stack trace utility like xdebug, as far
as I know. However, you can slightly modify your code to do this: <?php function error ($func,$message) { print "The error message is $message"; print "The function that called the error was: ".$func."\n"; } function bad_function($param) { error (__FUNCTION__,"This is dumb"); return false; } bad_function("blah"); ?>I also placed that online in my code library (to which I alwaysforget to link, but have dozens of examples), along with a function to list all of the user-level functions available to the current script.[Demo] http://www.pilotpig.net/code-library/function-info.php [Source] http://www.pilotpig.net/code-library/source.php?f=function-info.php