Professional Web Applications Themes

Function/Global var to return name of calling function? - PHP Development

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() {...

  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. #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_SPAMMERmindspring.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 (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  3. #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 <andyandyh.co.uk>, wrote:
    >>On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
    >><drhansenjrSMACK_A_SPAMMERmindspring.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

  4. #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 <andyandyh.co.uk>, wrote:
    >>On Sat, 05 Jul 2003 16:43:44 -0500, Daniel Hansen
    >><drhansenjrSMACK_A_SPAMMERmindspring.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

Similar Threads

  1. Replies: 3
    Last Post: March 19th, 07:59 PM
  2. Replies: 0
    Last Post: August 24th, 10:59 AM
  3. Global Function & Sub
    By Arne Tiedemann in forum ASP.NET Web Services
    Replies: 8
    Last Post: August 11th, 04:56 PM
  4. #24967 [Bgs]: fopen when placed within a function causes function to return
    By laurie at oneuponedown dot co dot uk in forum PHP Development
    Replies: 1
    Last Post: August 7th, 12:12 PM
  5. #24967 [NEW]: fopen when placed within a function causes function to return
    By laurie at oneuponedown dot co dot uk in forum PHP Development
    Replies: 0
    Last Post: August 6th, 09:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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