I am familiar with the get_defined_functions() function as a way of listing all of the user defined functions.

Is there a way with PHP to also determine from which file the user function was defined?

Consider the following example. Assume there are three files:
- index.php
- user_functions1.inc.php
- user_functions2.inc.php

contents of user_functions1.inc.php:

function example_function1() {
    print "hello world 1";

contents of user_functions2.inc.php:

function example_function2() {
    print "hello world 2";

contents of index.php:

$functions = get_defined_functions();
$user_functions = $functions['user'];
foreach($user_functions as $function) {
	print "$function<br>";

output of index.php:


I would like to use PHP to determine that user function example_function1 was defined in the file user_functions1.inc.php

Is this possible?

Thank you.


9 Years
Discussion Span
Last Post by veedeoo


I wrote a simple class in doing this job. The only limitations is that you will have to put all of your function files in certain directory.

I wrote this more than some 3 years ago, but I never have the chance to updgrade it. This class was inspired by unknown author that I came across long time ago. The original function was not capable of finding the exact file name of which the function is defined (it has to be included), I added a recursion in it. Today, I just notice that it was not a best choice for me to do a recusrsion.

Here is the class, use it if you think it will be of any help, ELSE; just ignore it.. WARNING! Not the best class I wrote.. I was only 16 when I originally thought about this, but I was not able to make it work until 2 years ago. I did not change anything on it ever since. This class was also published here in Daniweb at one time..

the dreaded class :), here we go .. you are welcome to modify the class, and if you do please shar it..


## no revision since 2009, except for commenting the echo in the method in 2013.

#### You can save this class somewhere else and just include it to your script..

class Find_function {

private $_somedirectory;
private $_somefunction;

function __construct($inDirectory,$myFunction){

$this->_somedirectory = $inDirectory;
$this->_somefunction =  $myFunction;
function findMyfunction(){

$theSearchRes = '';

if($thisHandle = opendir($this->_somedirectory)){
    while (false !== ($thisItem = readdir($thisHandle))){
      $thisDirItem = $this->_somedirectory.'/'.$thisItem ;
        if(($thisItem != ".") && ($thisItem != "..")){
        if( is_dir( $thisDirItem )){
        ## watch out! recursion below !!!!
        $thisRec = findMyfunction();
        echo $thisRec.PHP_EOL;

        ## EOif 4
## EOiF 3
} else {
$itemExt = substr(strtolower(strrchr($thisItem, '.')), 1);
if($itemExt == 'php'){
$readItem =  file_get_contents($thisDirItem);
$findFunctionLine = strpos($readItem, $this->_somefunction);

if($findFunctionLine == false){


} else {

## if function found, print it. commented in 2013 un-comment for testing
//echo "The function called '$this->_somefunction' was found in '$thisDirItem ', and positioned at approx. $findFunctionLine <br />";

return array(true,$thisDirItem);



## EOiF 2

## EOwhile
## EOiF 1
$theSearchRes = ob_get_contents();
return $theSearchRes;
## EOF

## EOC

How to use it?????

assuming all of your function files are located in directory called functions, then we can create an object or an instance of the class like this

$newObject = new Find_function( 'functions', 'example_function1' );

If you want to include the function file if it exists, then do it like this

 echo $newObject->findMyfunction()[1];

 echo '<br/>';

The above implementation should deliver the text string

hello world 1

One last thing, comment the echo from the class if you want the class to return a boolean reponse. I am pretty sure you are more than capable of doing so.

Edited by veedeoo: info added


Please update the class with visibilities as public, just add public in the front.. like this.

public function __construct
public function findMyfunction
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.