Ask a Question related to PHP Development, Design and Development.
-
Daniel Hansen #1
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() {
Daniel Hansen Guest
-
#40509 [NEW]: key() function changed behaviour if global array is used within function
From: alex dot killing at gmx dot de Operating system: MaxOsX/RedHat PHP version: 5.2.1 PHP Bug Type: Arrays related Bug... -
Calling a function within a function registered to an event
Hey, Just registered and tried to search through the forum for a solution to my problem, but I haven't found anything that could help me. I've... -
Global Function & Sub
Hi All, i'm new in this Forum and in programming ASP Pages. My Invironment - Win2003 Server IIS6 asp enabled I'll use an functions.asp... -
#24967 [Bgs]: fopen when placed within a function causes function to return
ID: 24967 User updated by: laurie at oneuponedown dot co dot uk Reported By: laurie at oneuponedown dot co dot uk Status: ... -
#24967 [NEW]: fopen when placed within a function causes function to return
From: laurie at oneuponedown dot co dot uk Operating system: Linux PHP version: 4.3.2 PHP Bug Type: *Directory/Filesystem... -
Andy Hassall #2
Re: Function/Global var to return name of calling function?
On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
<drhansenjrSMACK_A_SPAMMER@mindspring.com> wrote:
[url]http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=59a3dbdba1fc2871&rnum=1[/url]>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?
--
Andy Hassall (andy@andyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
Space: disk usage analysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
Andy Hassall Guest
-
Daniel Hansen #3
Re: Function/Global var to return name of calling function?
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 <andy@andyh.co.uk>, wrote:
------------------------------------------------>>On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
>><drhansenjrSMACK_A_SPAMMER@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?
>>[url]http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=59a3dbdba1fc2871&rnum=1[/url]
Dan Hansen
------------------------------------------------
Daniel Hansen Guest
-
Daniel Hansen #4
Re: Function/Global var to return name of calling function?
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 <andy@andyh.co.uk>, wrote:
------------------------------------------------>>On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
>><drhansenjrSMACK_A_SPAMMER@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?
>>[url]http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=59a3dbdba1fc2871&rnum=1[/url]
Dan Hansen
------------------------------------------------
Daniel Hansen Guest



Reply With Quote

