Hướng dẫn find the correct answer to invoke a method in php - tìm câu trả lời chính xác để gọi một phương thức trong php

Tôi chỉ đã viết một phiên bản này có tên là "Get_Caller", tôi hy vọng nó sẽ giúp. Của tôi là khá lười biếng. Bạn chỉ có thể chạy get_caller () từ một hàm, bạn không cần phải chỉ định nó như thế này:

get_caller(__FUNCTION__);

Đây là tập lệnh đầy đủ với một trường hợp thử nghiệm kỳ quặc:

<?php

/* This function will return the name string of the function that called $function. To return the
    caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
*/
function get_caller($function = NULL, $use_stack = NULL) {
    if ( is_array($use_stack) ) {
        // If a function stack has been provided, used that.
        $stack = $use_stack;
    } else {
        // Otherwise create a fresh one.
        $stack = debug_backtrace();
        echo "\nPrintout of Function Stack: \n\n";
        print_r($stack);
        echo "\n";
    }

    if ($function == NULL) {
        // We need $function to be a function name to retrieve its caller. If it is omitted, then
        // we need to first find what function called get_caller(), and substitute that as the
        // default $function. Remember that invoking get_caller() recursively will add another
        // instance of it to the function stack, so tell get_caller() to use the current stack.
        $function = get_caller(__FUNCTION__, $stack);
    }

    if ( is_string($function) && $function != "" ) {
        // If we are given a function name as a string, go through the function stack and find
        // it's caller.
        for ($i = 0; $i < count($stack); $i++) {
            $curr_function = $stack[$i];
            // Make sure that a caller exists, a function being called within the main script
            // won't have a caller.
            if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                return $stack[$i + 1]["function"];
            }
        }
    }

    // At this stage, no caller has been found, bummer.
    return "";
}

// TEST CASE

function woman() {
    $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
    if ($caller != "") {
        echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
    } else {
        echo "no-one called ", __FUNCTION__, "()\n";
    }
}

function man() {
    // Call the woman.
    woman();
}

// Don't keep him waiting
man();

// Try this to see what happens when there is no caller (function called from main script)
//woman();

?>

Man () gọi Woman (), người gọi get_caller (). get_caller () chưa biết ai đã gọi nó, bởi vì người phụ nữ () đã thận trọng và không nói với nó, vì vậy nó tái diễn để tìm hiểu. Sau đó, nó trở về người gọi là phụ nữ (). Và bản in ở chế độ mã nguồn trong trình duyệt hiển thị ngăn xếp hàm:

Printout of Function Stack: 

Array
(
    [0] => Array
        (
            [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
            [line] => 46
            [function] => get_caller
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
            [line] => 56
            [function] => woman
            [args] => Array
                (
                )

        )

    [2] => Array
        (
            [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
            [line] => 60
            [function] => man
            [args] => Array
                (
                )

        )

)

man() called woman(). No surprises there.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    The code consist of multiple functions performing different tasks but associated with each other directly or indirectly, and suddenly display an error in some of the functions then it will become necessary to find the name of the function in which an error has been occurred.

    Approach:

    • Khai báo một hàm do người dùng xác định CallfunctionName () sẽ đóng vai trò là hàm trình gỡ lỗi (nó theo dõi hàm khác trong tập lệnh).
    • Tạo tham chiếu của lớp ngoại lệ bằng cách sử dụng từ khóa ‘mới.
    • Bây giờ sử dụng biến tham chiếu này, hãy gọi hàm sẵn có GetTrace () (là hàm thành viên của lớp ngoại lệ).
    • Tìm nạp giá trị được trả về bởi hàm getTrace () trong bất kỳ chức năng nào (getTrace () trả về mảng kết hợp).
    • In biến đó bằng hàm print_r ().
    • Tạo một hoặc nhiều chức năng do người dùng xác định để kiểm tra chức năng theo dõi.

    Ví dụ 1:

    <?php

    function CallingFunctionName() {

        $ex

    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    0
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    1
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    2

        

    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    4
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    0$ex
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    7

        

    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    9
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    0
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    4
    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    2

        

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    4
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    9
    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    6

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    7

    function

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    9
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    0
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    1
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    2
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    3

        

    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    5
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    0
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    1
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    2
    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    6

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    7

    function

    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    5
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    0
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    1
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    2
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    3

        

    Array
    (
        [file] => /home/1b5518d5af0615813238e7534ccb6e8a.php
        [line] => 38
        [function] => firstCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    8

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    7

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    9<?php1
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    1<?php3
    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    6

    <?php5

    Output:

    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    

    Giải thích: Hàm GetTrace () sẽ quét toàn bộ phương thức CallStack và lưu trữ tất cả thông tin dưới dạng một mảng kết hợp. Mảng liên kết mang thông tin sau. The getTrace() function will scan whole method callstack and stores all the information in the form of an associative array. Associative array carries the following information.

    • [Tệp]: Đường dẫn tuyệt đối của tệp PHP hiện tại. Absolute path of the current PHP file.
    • [dòng]: Số dòng trong đó hàm ‘CallingFunctionName () đã được gọi. Line number where the function ‘CallingFunctionName()’ has been called.
    • [Hàm]: mang tên của chức năng gọi. Carries the name of calling function.
    • [args]: đưa ra các giá trị của các đối số gọi hàm. Gives the values of arguments that calling the function.

    Trên dòng số 15, chuyển giá trị 1 dưới dạng chỉ số của Trace [], do đó, nó trả về tất cả thông tin của hàm trên hầu hết trong ngăn xếp.

    Ví dụ 2:

    <?php

    function CallingFunctionName() {

        $ex

    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    0
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    1
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    2

        

    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    4
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    0$ex
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    7

        

    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    9
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    0
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    4
    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    2

        

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    4
    <?php
    
    /* This function will return the name string of the function that called $function. To return the
        caller of your function, either call get_caller(), or get_caller(__FUNCTION__).
    */
    function get_caller($function = NULL, $use_stack = NULL) {
        if ( is_array($use_stack) ) {
            // If a function stack has been provided, used that.
            $stack = $use_stack;
        } else {
            // Otherwise create a fresh one.
            $stack = debug_backtrace();
            echo "\nPrintout of Function Stack: \n\n";
            print_r($stack);
            echo "\n";
        }
    
        if ($function == NULL) {
            // We need $function to be a function name to retrieve its caller. If it is omitted, then
            // we need to first find what function called get_caller(), and substitute that as the
            // default $function. Remember that invoking get_caller() recursively will add another
            // instance of it to the function stack, so tell get_caller() to use the current stack.
            $function = get_caller(__FUNCTION__, $stack);
        }
    
        if ( is_string($function) && $function != "" ) {
            // If we are given a function name as a string, go through the function stack and find
            // it's caller.
            for ($i = 0; $i < count($stack); $i++) {
                $curr_function = $stack[$i];
                // Make sure that a caller exists, a function being called within the main script
                // won't have a caller.
                if ( $curr_function["function"] == $function && ($i + 1) < count($stack) ) {
                    return $stack[$i + 1]["function"];
                }
            }
        }
    
        // At this stage, no caller has been found, bummer.
        return "";
    }
    
    // TEST CASE
    
    function woman() {
        $caller = get_caller(); // No need for get_caller(__FUNCTION__) here
        if ($caller != "") {
            echo $caller , "() called " , __FUNCTION__ , "(). No surprises there.\n";
        } else {
            echo "no-one called ", __FUNCTION__, "()\n";
        }
    }
    
    function man() {
        // Call the woman.
        woman();
    }
    
    // Don't keep him waiting
    man();
    
    // Try this to see what happens when there is no caller (function called from main script)
    //woman();
    
    ?>
    
    9
    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    6

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    7

    function

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    9
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    0
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    1
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    2
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    3

        

    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    5
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    0
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    1
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    2
    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    6

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    7

    function

    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    5
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    0
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    1
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    2
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    3

        

    Array
    (
        [file] => /home/1b5518d5af0615813238e7534ccb6e8a.php
        [line] => 38
        [function] => firstCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    8

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    7

    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    9<?php1
    Array
    (
        [file] => /home/5aeb55f2023e4e59fedcedc30e37e060.php
        [line] => 26
        [function] => secondCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    
    1<?php3
    Printout of Function Stack: 
    
    Array
    (
        [0] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 46
                [function] => get_caller
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 56
                [function] => woman
                [args] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [file] => /Users/Aram/Development/Web/php/examples/get_caller.php
                [line] => 60
                [function] => man
                [args] => Array
                    (
                    )
    
            )
    
    )
    
    man() called woman(). No surprises there.
    
    6

    <?php5

    Output:

    Array
    (
        [file] => /home/1b5518d5af0615813238e7534ccb6e8a.php
        [line] => 38
        [function] => firstCall
        [args] => Array
            (
                [0] => test
                [1] => php
            )
    
    )
    

    Giải thích: Hàm GetTrace () sẽ quét toàn bộ phương thức CallStack và lưu trữ tất cả thông tin dưới dạng một mảng kết hợp. Mảng liên kết mang thông tin sau.: The output contains all the information of function firstcall() because the value of subscript trace is 2.

    • [Tệp]: Đường dẫn tuyệt đối của tệp PHP hiện tại. Position 0 would be the function itself (here CallingFunctionName()).
    • [dòng]: Số dòng trong đó hàm ‘CallingFunctionName () đã được gọi. Position 1 would be the function secondcall because this is the top most function in callstack.
    • [Hàm]: mang tên của chức năng gọi. Position 2 would be the function ‘firstcall’ because this is the top 1 function in callstack.
    • [args]: đưa ra các giá trị của các đối số gọi hàm. Position n would be the nth function because this is the top nth function in callstack.

    Làm thế nào các chức năng được tạo và gọi trong PHP?

    Tạo và gọi hàm trong PHP, tên hàm là bất kỳ tên nào kết thúc trong dấu ngoặc đơn mở và đóng. Hàm từ khóa thường được sử dụng để bắt đầu một tên hàm. Để gọi một chức năng, chỉ cần nhập tên của nó theo sau là dấu ngoặc đơn.To invoke a function, simply type its name followed by the parenthesis.

    Làm thế nào để bạn tìm ra chức năng người gọi trong PHP?

    [Hàm]: mang tên của chức năng gọi ...
    Khai báo một hàm do người dùng xác định CallfunctionName () sẽ đóng vai trò là hàm trình gỡ lỗi (nó theo dõi hàm khác trong tập lệnh) ..
    Tạo tham chiếu của lớp ngoại lệ bằng cách sử dụng từ khóa 'mới' ..

    Phương pháp trong PHP là gì?

    Phương pháp được sử dụng để thực hiện các hành động.Trong lập trình theo định hướng đối tượng trong PHP, các phương thức là các chức năng bên trong các lớp.Tuyên bố và hành vi của họ gần như tương tự như các chức năng bình thường, ngoại trừ việc sử dụng đặc biệt của họ bên trong lớp.Hãy nhắc nhở vai trò của một chức năng.used to perform actions. In Object Oriented Programming in PHP, methods are functions inside classes. Their declaration and behavior are almost similar to normal functions, except their special uses inside the class. Let's remind the role of a function.

    Chức năng trong PHP với ví dụ là gì?

    Các hàm PHP tương tự như các ngôn ngữ lập trình khác.Một hàm là một đoạn mã có thêm một đầu vào dưới dạng tham số và thực hiện một số xử lý và trả về một giá trị.Bạn đã thấy nhiều chức năng như fopen () và fread (), v.v.