2

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)

tereško
  • 58,060
  • 25
  • 98
  • 150
Nuvolari
  • 1,103
  • 5
  • 13
  • 29
  • only way i see of doing this is playing with regular expressions .. – Gntem Feb 16 '13 at 07:59
  • You can use `get_class_methods` it will output all public methods of class XYZ in array. – Basic Bridge Feb 16 '13 at 08:08
  • Yes true but that requires I know the name of the class first. I want a way to 'detect' classes without first knowing their names from a file. – Nuvolari Feb 16 '13 at 08:15
  • You could assign to a user, say, level as integer. Store it in database, fetch when user is logging in. Then you could check the level and display allowed things: inputs, buttons. Like if ($user->level >= GUEST_LEVEL) echo "You have a permission to do so and so" ; Of course, you must check the level in your logic engine besides the graphic things. – sybear Feb 16 '13 at 08:26
  • Interesting idea atm I have only view and write settings (ie allow them to add/modify db stuff) – Nuvolari Feb 16 '13 at 08:50

2 Answers2

5

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\n";
}
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • Looks promising. Is this a better method then using the Token way http://php.net/token_get_all that I saw here: http://stackoverflow.com/questions/928928/determining-what-classes-are-defined-in-a-php-class-file – Nuvolari Feb 16 '13 at 08:49
  • 1
    @Nuvolari Well, it pretty much depends on what you want to do with it. I personally prefer this version (since it's much shorter and simpler), though - as a con - keep in mind that a full `include` occurs every time we'd want to analyze a script. – Dr.Kameleon Feb 16 '13 at 09:34
  • Well I'd be analysing anywhere between 10 and 1000+ script files all at once and inputting their classes/methods into the db. – Nuvolari Feb 16 '13 at 09:39
  • @Nuvolari Well, if it's a one-time thing (even if 1000+ scripts are quite a lot), I'd still stick to this method. If it's something supposed to be constantly running on some server, then I'd go for something else. Just my .2 cents... – Dr.Kameleon Feb 16 '13 at 09:44
  • Its more of a one time or occasional thing. – Nuvolari Feb 16 '13 at 16:43
1
<?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 />";
}

?>
Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
  • This code works, but doesn't account for whitespace between the function name and the curly bracket. E.g: `public function test() {` will work, but `public function test() [new line] {' will not.` – Simeon Mar 31 '15 at 22:54