Function List of PHP File

Finding all PHP functions in a file

I had THE idea 2 seconds after writing the question.

$data = file_get_contents($file);   

$data = preg_replace("/<script[^>]*>[\s\S]*?<\/script>/", "", $data);
preg_match_all("/function[\s\n]+(\S+)[\s\n]*\(/", $data, $outputData);

Just delete all the <script>-Tags!
If you would need them later, you also could save them (instead of replacing them) and add them later!

Just if someone else will have the same problem!

How to list all PHP variables in a file?

Use file_get_contents() and preg_match_all():

$file = file_get_contents('file.php'); 
preg_match_all('/\$[A-Za-z0-9-_]+/', $file, $vars);

print_r($vars[0]);

Get a called functions list in PHP

I was trying to achieve what you want and finally came up with an reasonable solution.

Make a class named Debug and include that above every file you want to debug in. Build yourself a function that prints nicely the information stored in $calls.

class Debug {
private static $calls;

public static function log($message = null)
{
if(!is_array(self::$calls))
self::$calls = array();

$call = debug_backtrace(false);
$call = (isset($call[1]))?$call[1]:$call[0];

$call['message'] = $message;
array_push(self::$calls, $call);
}
}

Call this function everytime you declare a function first line in the functionbody: Debug::log($message(optional) )

PHP: Documentation of function list

To get paramater number and names, you can use Reflection in PHP :

function getFunctionArgumentNames($functionName) {
$reflection = new ReflectionFunction($functionName);
$argumentNames= array();
foreach ($reflection ->getParameters() as $parameter) {
$argumentNames[] = $parameter->name;
}
return $argumentNames;
}

Then you'll have your parameter names, and the length of the returned array will give you how many there are.


More information can be found about it :

  • ReflectionClass
  • ReflectionObject
  • What is Reflection in PHP ?

    Another common use of Reflection is for creating documentation. It would be extremely labour intensive to write documentation about every method of every class of a large framework or application. Instead, Reflection can automatically generate the documentation for you. It does this by inspecting each method, constructor and class to determine what goes in and what comes out.

how can i print the array of all defined functions?

You can print defined functions as follows:

$arr = get_defined_functions();

print_r($arr);

Documentation here

PHP function to list folders and file, but detect when is a folder or file

This should work for you! Directorys are in h1 and files in a tags:

(I'm checking with the functionis_dir if it's a directory an if it is i'm outputing the name in h1 and otherwise in a tags)

<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li>';

if(is_dir($dir.'/'.$ff))
echo "<h1>" . $ff . "</h1>";
else
echo "<a href=\"#\">" . $ff . "</a><br />";

if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
echo '</li>';
}
}
echo '</ol>';
}

?>

If you don't want it in a list use this:

<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){

if(is_dir($dir.'/'.$ff))
echo "<h1>" . $ff . "</h1>";
else
echo "<a href=\"#\">" . $ff . "</a><br />";

if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
}
}

}

?>


Related Topics



Leave a reply



Submit