Skip to main content
added 365 characters in body
Source Link
Andrew Moore
  • 95.7k
  • 30
  • 167
  • 175
function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);
    
    $functions = array();
    $nextStringIsFunc = false;
    $inClass = false;
    $bracesCount = 0;

    foreach($tokens as $token) {
        switch($token[0]) {
            case T_CLASS:
                $inClass = true;
                break;
            case T_FUNCTION:
                if(!$inClass) $nextStringIsFunc = true;
                break;
            
            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;
            
            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;
                
            // Exclude Classes
            case '{':
                if($inClass) $bracesCount++;
                break;
            
            case '}':
                if($inClass) {
                    $bracesCount--;
                    if($bracesCount === 0) $inClass = false;
                }
                break;
        }
    }
    
    return $functions;
}
function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);
    
    $functions = array();
    $nextStringIsFunc = false;

    foreach($tokens as $token) {
        switch($token[0]) {
            case T_FUNCTION:
                $nextStringIsFunc = true;
                break;
            
            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;
            
            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;
        }
    }
    
    return $functions;
}
function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);
    
    $functions = array();
    $nextStringIsFunc = false;
    $inClass = false;
    $bracesCount = 0;

    foreach($tokens as $token) {
        switch($token[0]) {
            case T_CLASS:
                $inClass = true;
                break;
            case T_FUNCTION:
                if(!$inClass) $nextStringIsFunc = true;
                break;
            
            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;
            
            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;
                
            // Exclude Classes
            case '{':
                if($inClass) $bracesCount++;
                break;
            
            case '}':
                if($inClass) {
                    $bracesCount--;
                    if($bracesCount === 0) $inClass = false;
                }
                break;
        }
    }
    
    return $functions;
}
added 124 characters in body
Source Link
Andrew Moore
  • 95.7k
  • 30
  • 167
  • 175

You can get a list of currently defined function by using get_defined_functions():

$arr = get_defined_functions();
var_dump($arr['user']);

Internal functions are at index internal while user-defined function are at index user.

Note that this will output all functions that were declared previous to that call. Which means that if you include() files with functions, those will be in the list as well. There is no way of separating functions per-file other than making sure that you do not include() any file prior to the call to get_defined_functions().


If you must have the a list of functions per file, the following will attempt to retrieve a list of functions by parsing the source.

function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);
    
    $functions = array();
    $nextStringIsFunc = false;
    
    foreach($tokens as $token) {
        switch($token[0]) {
            case T_FUNCTION:
                $nextStringIsFunc = true;
                break;
            
            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;
            
            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;
        }
    }
    
    return $functions;
}

Use at your own risk.

You can get a list of currently defined function by using get_defined_functions():

$arr = get_defined_functions();
var_dump($arr['user']);

Internal functions are at index internal while user-defined function are at index user.

Note that this will output all functions that were declared previous to that call. Which means that if you include() files with functions, those will be in the list as well. There is no way of separating functions per-file other than making sure that you do not include() any file prior to the call to get_defined_functions().


If you must have the a list of functions per file, the following will attempt to retrieve a list of functions by parsing the source.

function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);
    
    $functions = array();
    $nextStringIsFunc = false;
    
    foreach($tokens as $token) {
        switch($token[0]) {
            case T_FUNCTION:
                $nextStringIsFunc = true;
                break;
            
            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;
        }
    }
    
    return $functions;
}

Use at your own risk.

You can get a list of currently defined function by using get_defined_functions():

$arr = get_defined_functions();
var_dump($arr['user']);

Internal functions are at index internal while user-defined function are at index user.

Note that this will output all functions that were declared previous to that call. Which means that if you include() files with functions, those will be in the list as well. There is no way of separating functions per-file other than making sure that you do not include() any file prior to the call to get_defined_functions().


If you must have the a list of functions per file, the following will attempt to retrieve a list of functions by parsing the source.

function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);
    
    $functions = array();
    $nextStringIsFunc = false;

    foreach($tokens as $token) {
        switch($token[0]) {
            case T_FUNCTION:
                $nextStringIsFunc = true;
                break;
            
            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;
            
            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;
        }
    }
    
    return $functions;
}

Use at your own risk.

added 219 characters in body
Source Link
Andrew Moore
  • 95.7k
  • 30
  • 167
  • 175

You can get a list of currently defined function by using get_defined_functions():

$arr = get_defined_functions();
var_dump($arr['user']);

Internal functions are at index internal while user-defined function are at index user.

Note that this will output all functions that were declared previous to that call. Which means that if you include() files with functions, those will be in the list as well. There is no way of separating functions per-file other than making sure that you do not include() any file prior to the call to get_defined_functions().


If you must have the a list of functions per file, the following will attempt to retrieve a list of functions by parsing the source. It is nowhere near perfect. It doesn't check for <?php ?> boundaries and for string literals.

function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $matches$tokens = arraytoken_get_all($source);
    preg_match_all
    $functions = array('/function\s+?);
    $nextStringIsFunc = false;
    
    foreach([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$tokens as $token)\s*?\ {
        switch(/i',$token[0]) $source,{
 $matches           case T_FUNCTION:
                $nextStringIsFunc = true;
                break;
            
            case T_STRING:
                if($nextStringIsFunc); {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;
        }
    }
    
    return $matches[1];$functions;
}

Use at your own risk.

You can get a list of currently defined function by using get_defined_functions():

$arr = get_defined_functions();
var_dump($arr['user']);

Internal functions are at index internal while user-defined function are at index user.

Note that this will output all functions that were declared previous to that call. Which means that if you include() files with functions, those will be in the list as well. There is no way of separating functions per-file other than making sure that you do not include() any file prior to the call to get_defined_functions().


If you must have the a list of functions per file, the following will attempt to retrieve a list of functions by parsing the source. It is nowhere near perfect. It doesn't check for <?php ?> boundaries and for string literals.

function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $matches = array();
    preg_match_all('/function\s+?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+)\s*?\(/i', $source, $matches);
    
    return $matches[1];
}

Use at your own risk.

You can get a list of currently defined function by using get_defined_functions():

$arr = get_defined_functions();
var_dump($arr['user']);

Internal functions are at index internal while user-defined function are at index user.

Note that this will output all functions that were declared previous to that call. Which means that if you include() files with functions, those will be in the list as well. There is no way of separating functions per-file other than making sure that you do not include() any file prior to the call to get_defined_functions().


If you must have the a list of functions per file, the following will attempt to retrieve a list of functions by parsing the source.

function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);
    
    $functions = array();
    $nextStringIsFunc = false;
    
    foreach($tokens as $token) {
        switch($token[0]) {
            case T_FUNCTION:
                $nextStringIsFunc = true;
                break;
            
            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;
        }
    }
    
    return $functions;
}

Use at your own risk.

added 556 characters in body
Source Link
Andrew Moore
  • 95.7k
  • 30
  • 167
  • 175
Loading
Source Link
Andrew Moore
  • 95.7k
  • 30
  • 167
  • 175
Loading