johnlemon
johnlemon

Reputation: 21459

Output calling function information in PHP

Consider this code :

class App {
    public static function log($msg) {
    echo $msg;
    //echo WHAT_CLASS_CALLED_LOG;
    //echo WHAT_LINE_CALLED_LOG;
}
}


class Tester {
public function Make() {
    App::log('test');
}
}


$obj = new Tester();
$obj->Make();

Is is possible to get the class name and the function name of the calling function in the log method ? ( without explicitly sending them to the log function )

PS : I think trigger_error works like that behind the curtains so I was wondering If I can achieve this.

Upvotes: 0

Views: 134

Answers (2)

Brad Christie
Brad Christie

Reputation: 101614

I believe you're looking for debug_backtrace.

Returns an associative array. The possible returned elements are as follows: 

Name        Type    Description
function    string  The current function name. See also __FUNCTION__.
line        integer The current line number. See also __LINE__.
file        string  The current file name. See also __FILE__.
class       string  The current class name. See also __CLASS__
object      object  The current object.
type        string  The current call type. If a method call, "->" is returned. If a static method call, "::" is returned. If a function call, nothing is returned.
args        array   If inside a function, this lists the functions arguments. If inside an included file, this lists the included file name(s). 

Upvotes: 3

Fidi
Fidi

Reputation: 5824

May be debug_backtrace could help. Check the PHP-Manual for a function-description:

http://de.php.net/manual/en/function.debug-backtrace.php

Upvotes: 2

Related Questions