Hello,

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:

<?php
function example_function1() {
    print "hello world 1";
}
?>


contents of user_functions2.inc.php:

<?php
function example_function2() {
    print "hello world 2";
}
?>

contents of index.php:

<?php
include(user_functions1.inc.php);
include(user_functions2.inc.php);
$functions = get_defined_functions();
$user_functions = $functions['user'];
foreach($user_functions as $function) {
	print "$function<br>";
}
?>

output of index.php:

example_function1
example_function2

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.

Dave

Recommended Answers

All 4 Replies

i guess u should do it on each file

Hi,

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..

<?php

## 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 = '';
ob_start();

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();
        if(isset($thisRec)){
        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){

continue;

} 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);

}
}else{


continue;
}
}

## EOiF 2
}

## EOwhile
}
closedir($thisHandle);
## EOiF 1
}
$theSearchRes = ob_get_contents();
ob_end_clean();
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

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

 include_once($newObject->findMyfunction()[1]);
 echo '<br/>';
 example_function1();
}

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.

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

public function __construct
public function findMyfunction
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.