Calling function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    Calling function

    Hello All,
    Is there any way to call the mother function?

    I mean
    Code:
    <?php
     function a()
     {
      
      b();
     }
     function b()
     {
      echo __MOTHER_FUNCTION__;
     }
    ?>
    expected out put is
    Code:
     a()
    is there anything like that?

    Please let me know
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Do you mean get the name of the function that the current function was called by?

    If so, defining it a Mother function is incorrect terminology.
    I define it simply as a calling function.

    I am not aware of a php function that does this.
    A work around would be to pass a parameter as a marker.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      hey green,
      i thought about that too, passing the function name. But I was looking for if any option available to access the function stack.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Well, for debugging purposes you can use debug_backtrace.

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          In fact I did saw that function before.

          But the problem is I have called some of my function from lots of different files. The problem occurs when a silly query error occurs i can figure out at what function I am getting the error. But It sometime become a head ache from which other function i have called it.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            shouldn’t that be given in the stacktrace? (ok, I only use stacktraces from exceptions, but that should not be much different from debug_backtrace ())

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              But the problem is I have called some of my function from lots of different files
              It may help your problem using one of the file functions or pre-defined constants to identify the calling FILE.
              Can't be certain how, but there are plenty.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                usually the stacktrace lists all called functions, their file names, lines, etc.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Dormilich is right.

                  Code:
                  php > function a() { b(); }
                  php > function b() { c(); }
                  php > function c() { var_dump(debug_backtrace()); }
                  php > a();
                  
                  // Gives:
                  abcarray(3) {
                    [0]=>
                    array(4) {
                      ["file"]=>
                      string(14) "php shell code"
                      ["line"]=>
                      int(1)
                      ["function"]=>
                      string(1) "c"
                      ["args"]=>
                      array(0) {
                      }
                    }
                    [1]=>
                    array(4) {
                      ["file"]=>
                      string(14) "php shell code"
                      ["line"]=>
                      int(1)
                      ["function"]=>
                      string(1) "b"
                      ["args"]=>
                      array(0) {
                      }
                    }
                    [2]=>
                    array(4) {
                      ["file"]=>
                      string(14) "php shell code"
                      ["line"]=>
                      int(1)
                      ["function"]=>
                      string(1) "a"
                      ["args"]=>
                      array(0) {
                      }
                    }
                  }

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    I have to mention that I prefer Exceptions, simply because I can control the programme flow. (no need to go through the database code if the connection fails)

                    Comment

                    • johny10151981
                      Top Contributor
                      • Jan 2010
                      • 1059

                      #11
                      Thanks for you replies, I will study your suggestions

                      Comment

                      Working...