I would like to output to an array all classes and their methods names as well as method type (public, private, protected) to an array after picking a particular script file.
This will be used to create a permissions system, where an admin can specify what classes a particular user group may access.
For instance say in the script file test.php it has the following:
class test{
public function dostuff(){
}
private function hide(){
}
}
It should give me a list like this: class: test methods: dostuff (public) hide (private)
OK, this is what I'd suggest :
// Get new class name
$classes = get_declared_classes();
include 'your_php_file.php';
$diff = array_diff(get_declared_classes(), $classes);
$class = reset($diff);
// Get class's methods
$methods = get_class_methods($class);
// Print them out
echo "Class : ".$class;
foreach ($methods as $method) {
echo "$method
";
}
<?php
$file="test_class.php";
$fp = fopen($file, 'r');
$class = $buffer = '';
$method = $buffer = '';
while(!feof($fp))
{
$buffer .= fread($fp, 512);
if (preg_match('/class\s+(\w+)(.*)?\{/', $buffer, $matches)) {
$class = $matches[1];
//break;
}
if (preg_match_all('/function\s+(\w+)(.*)?\{/', $buffer, $match)) {
$method = $match[1];
//print_r($match);
//break;
}
}
echo "class:".$class."<br />";
//print_r($method);
foreach($method as $key=>$val)
{
echo "method : ".$val."<br />";
}
?>