开发者

How to get class/function/line name in a function outside a class

开发者 https://www.devze.com 2023-03-15 04:02 出处:网络
Example: class myExample { public function example_fn() { $example_val = $this->some_fn(); _debug( $example_val, _get_position() );

Example:

class myExample
{
    public function example_fn()
    {
        $example_val = $this->some_fn();
        _debug( $example_val, _get_position() );
    }
}

/**
 * Debug function
 */
function _debug( $input, $position )
{
    $output  = '<strong>Inside: '.$position.'</strong><br />';
    $output .= '<pre>';
    $output .= print_r( var_export( $input, true ), true );
    $output .= '</pre>';

    return print $output;
}

/**
 * Position function
 */
function _get_position()
{
    return 'Class: '.__CLASS__.' // Function: '.__FUNCTION__.' // Line: '.__LINE__;
}

Question:

With the current setup, the output returns the values of the position where the _get_position() function was defined. Can I somehow get the Class/function/line from where the _get_position开发者_如何学JAVA() function was called?

Thank you!


You can use http://www.php.net/manual/en/function.debug-backtrace.php


You got to use debug_backtrace for that:

function _get_position()
{
    $stack = debug_backtrace();
    return 'Class: '.$stack[1]['class'].' // Function: '.$stack[1]['function'].' // Line: '.$stack[0]['line'];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号