473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function/Global var to return name of calling function?

I'm sure I saw this somewhere but can't remember where and can't find it now...

Is there a PHP function or global variable that will return name of the calling function? I want to do this for error reporting purposes without
having to hardcode the function name into my scripts.

Say the function is named getFunctionName(). I want to do something like...

function foo() {
Jul 16 '05 #1
3 25076
On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
<dr***********************@mindspring.com> wrote:
I'm sure I saw this somewhere but can't remember where and can't find it now...

Is there a PHP function or global variable that will return name of the calling function?


http://groups.google.co.uk/groups?hl...1fc2871&rnum=1

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2
Like way kewl, dewd...

<?php
function a() {
$c = getFunctionName();
print "The current function is $c<br>\n";
}

function getFunctionName() {
$backtrace = debug_backtrace();
return $backtrace[1]['function'];
}
a();
?>

Works like dream!

Thanks!


On Sun, 06 Jul 2003 00:36:33 +0100, Andy Hassall <an**@andyh.co.uk>, wrote:
On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
<dr***********************@mindspring.com> wrote:
I'm sure I saw this somewhere but can't remember where and can't find it now...

Is there a PHP function or global variable that will return name of the calling function?


http://groups.google.co.uk/groups?hl...1fc2871&rnum=1


------------------------------------------------
Dan Hansen
------------------------------------------------
Jul 16 '05 #3
I **LOVE** it!

The code below generates the following log file entry (contants starting with "G" are defined elsewhere:

2003.07.06 07:20:13 (Sunday): Error in function: badfunction [Line 10; File: /var/www/html/app_name/core/phpscratch/caller.php3]<-b<-a; Error ID: 99
Description: I blew up!

Code:

// ************************************************** **
function a() {
b();
}

// ************************************************** **
function b() {
badFunction();
}

// ************************************************** **
function badFunction() {
logError(99, "I blew up!");
print "LOG ENTRY ATTEMPT DONE<br>";
print GerrorLog . "<br>";
}

// ************************************************** **
function logError($errID = 0, $description = "") {
$backtrace = debug_backtrace();

$callPath = $backtrace[1]['function'];
$callPath .= ' [Line ' . $backtrace[1]['line'];
$callPath .= '; File: ' . $backtrace[1]['file'] . ']';

for ($n=2; $n<=20; $n++) {
if (isset($backtrace[$n]['function'])) {
$callPath .= '<-' . $backtrace[$n]['function'];
}
}

$logEntry = date("Y.m.d H:i:s (l)") . ": Error in function: " . $callPath;
if ( strlen($description) > 0 ) {
$logEntry .= "; Error ID: $errID Description: " . $description;
}
$logEntry .= "\n";
logWrite($logEntry);
}

// ************************************************** **
function logWrite($logEntry) {
if ($theFile = fopen(GerrorLog,"a")) {
$bytesWritten = fputs($theFile, $logEntry);
if ($bytesWritten <= 0) {
print ("**ERROR: WRITING TO [APP_NAME] ERROR LOG<BR>\n");
}
} else {
print ("**ERROR: WRITING TO [APP_NAME] ERROR LOG<BR>\n");
}
// Send a notification e-mail
$to=GdefaultEMail;
$headers = "From: ".GdefaultEMail."\n";
$subj = "[APP_NAME] System Error Notification\n";
$body = $logEntry;
mail($to,$subj,$body,$headers);
}

// ************************************************** **
a();
// ************************************************** **

On Sun, 06 Jul 2003 00:36:33 +0100, Andy Hassall <an**@andyh.co.uk>, wrote:
On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
<dr***********************@mindspring.com> wrote:
I'm sure I saw this somewhere but can't remember where and can't find it now...

Is there a PHP function or global variable that will return name of the calling function?


http://groups.google.co.uk/groups?hl...1fc2871&rnum=1


------------------------------------------------
Dan Hansen
------------------------------------------------
Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4935
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
1944
by: jeff | last post by:
I have a php file that contains a couple of arrays used for state/country pull-down lists. I have two global arrays and an accessor method for each. I have some simple logging methods, so I know a...
8
2172
by: Ron_Adam | last post by:
Is there a way to hide global names from a function or class? I want to be sure that a function doesn't use any global variables by mistake. So hiding them would force a name error in the case...
3
14908
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
2523
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could...
12
3257
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
7
1936
by: Spoon | last post by:
Hello, Consider the following code: int foo(int n) { return n+2; } class bar { public: int x;
1
2194
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python...
12
2996
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.