Function/Global var to return name of calling function?

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. #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...
    2. 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...
    3. 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...
    4. #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: ...
    5. #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...
  3. #2

    Default 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:
    >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]

    --
    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

  4. #3

    Default 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

  5. #4

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139