Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine where a function has been called with PHP

Tags:

php

do you guys know how can i determine from what file was a function called inside of that function?

I was thinking of using debug_backtrace .. but that does not look as an elegant way to do it, and they also enumarate other reasons in another question here

So what other alternatives are there ?

thanks a lot

like image 448
Gabriel Solomon Avatar asked Apr 13 '09 11:04

Gabriel Solomon


4 Answers

I borrowed this code some time ago from somewhere and it works like a charm using debug_backtrace(). Hope you find it useful:

function backtrace(){
    $backtrace = debug_backtrace();

    $output = '';
    foreach ($backtrace as $bt) {
        $args = '';
        foreach ($bt['args'] as $a) {
            if (!empty($args)) {
                $args .= ', ';
            }
            switch (gettype($a)) {
                case 'integer':
                case 'double':
                    $args .= $a;
                    break;
                case 'string':
                    //$a = htmlspecialchars(substr(, 0, 64)).((strlen($a) > 64) ? '...' : '');
                    $args .= "\"$a\"";
                    break;
                case 'array':
                    $args .= 'Array('.count($a).')';
                    break;
                case 'object':
                    $args .= 'Object('.get_class($a).')';
                    break;
                case 'resource':
                    $args .= 'Resource('.strstr($a, '#').')';
                    break;
                case 'boolean':
                    $args .= $a ? 'TRUE' : 'FALSE';
                    break;
                case 'NULL':
                    $args .= 'Null';
                    break;
                default:
                    $args .= 'Unknown';
            }
        }
        $output .= '<br />';
        $output .= '<b>file:</b> '.@$bt['file'].' - line '.@$bt['line'].'<br />';
        $output .= '<b>call:</b> '.@$bt['class'].@$bt['type'].@$bt['function'].'('.$args.')<br />';
    }
    return $output;
}
like image 193
Seb Avatar answered Oct 23 '22 06:10

Seb


The responses here by the mickey mouse crew are typical of the online PHP community! Instead of sharing the answer they ask you why you might need it. Never mind the fact that solomongaby asks a valid question and it's a pretty normal feature to have in standard IDE's and more professional languages like Java and Objective-C.

Solomongaby, this is the simplest way to get you what you need:

$bt = debug_backtrace();
$end = end($bt);
var_dump($end['class']); //or var_dump($end['file']);
like image 24
PostCodeism Avatar answered Oct 23 '22 06:10

PostCodeism


From within the function debug_backtrace() is the only way to determine the caller, unless of course you pass that information as parameter.

Note, that you cannot use default values to do this. E.g.:

function f($param1, $param2, $caller=__FUNCTION__) ...

__FUNCTION__ will be evaluated at parse time, so it'll always have the same value. The function in which scope f is declared.

like image 38
vartec Avatar answered Oct 23 '22 05:10

vartec


You will not have many options there. The other option (which was posted in the other question) to force the caller to provde it's information with the function call (using PHP magic constants):

callFunction(1, 2, 3, __FUNCTION__)
callFunction(1, 2, 3, __CLASS__,  __METHOD__)
callFunction(1, 2, 3, $this,  __METHOD__)

  or

$class = new ReflectionClass( __CLASS__ ); 
$method = $class->getMethod( __METHOD__ );
callFunction(1, 2, 3, $method) // $method would be a ReflectionMethod obj

would be a possible alternative. But it's

  • obfuscating your code
  • can be "manipulated" which might cause the code of callFunction to fail and it would be quite difficult to track those errors down.

If I were in your place, I would try to avoid it in a function that is used throughout your code. use debug_backtrace (even if it might be 'slow'). Readable code wins over fast code.

like image 22
Marcel Jackwerth Avatar answered Oct 23 '22 05:10

Marcel Jackwerth