code blog

Get origin of function call

language: PHP

Get origin of function call

I needed to make a change to an API, and wanted to know what project files were calling a function. To do this within my code, I found this handy function:

This tells me the files involved with a function call.

example:

caller.php

<?php
include 'api.php';
api_function('argument');
?>

api.php

<?php
function api_function($string){
   $caller = debug_backtrace();
 
   //for last caller:
   echo "api_function() called from ".$caller[0]['file']." line ".$caller[0]['line'];
 
   //for all files included before caller, and a lot of other info:
   echo "api_function() called from:<pre>".print_r($caller,true)."</pre>";
 
   //optionall kill script at this point:
   exit;
 
   //... api_function()'s code
}
?>

User login