473,461 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Create 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 25071
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
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
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
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
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
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
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
by: Spoon | last post by:
Hello, Consider the following code: int foo(int n) { return n+2; } class bar { public: int x;
1
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
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
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.