RE: [PHP] Re: Getting the name of a function

From: Date: Wed, 27 Feb 2008 23:02:17 +0000
Subject: RE: [PHP] Re: Getting the name of a function
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-270475@lists.php.net to get a copy of this message
> -----Original Message----- > From: Shawn McKenzie [mailto:nospam@mckenzies.net] > Sent: Wednesday, February 27, 2008 3:58 PM > To: php-general@lists.php.net > Subject: [PHP] Re: Getting the name of a function > > Richard S. Crawford wrote: > > 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. For example: > > > > <?php > > function error ($message) { > > print "The error message is $message"; > > print "The function that called the error was: [INSERT COOL CODE > HERE]"; > > } > > > > function bad_function($param) { > > error ("This is dumb"); > > return false; > > } > > > > bad_function("blah"); > > ?> > > > > Ideally this script would print this out: > > > > The error message is This is dumb > > The function that called the error was bad_function > > > > 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? > > > It's an extension but may be worth a look: > http://us.php.net/manual/en/ref.apd.php > > -Shawn Not exactly what you want, but check the use of __FUNCTION__: function error ($message, $fn) { print "The error message is $message"; print "The function that called the error was: $fn"; } Function bad_function($param) { error ("This is dumb", __FUNCTION__); return false; } You can get better (and more complex) information using debug_backtrace http://php.net/debug_backtrace. Probably doing something like: function error ($message) { $backtrace = debug_backtrace(); $fn = $backtrace[1]["function"]; print "The error message is $message"; print "The function that called the error was: $fn"; } Function bad_function($param) { error ("This is dumb); return false; } Regards, Rob Andrés Robinet | Lead Developer | BESTPLACE CORPORATION  5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308 | TEL 954-607-4207 | FAX 954-337-2695 | Email: info@bestplace.net  | MSN Chat: best@bestplace.net  |  SKYPE: bestplace |  Web: bestplace.biz  | Web: seo-diy.com

« previous php.general (#270475) next »