Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Classes, Functions and Arguments in PHP

I want to create a function which receives a single argument that holds the path to a PHP file and then parses the given file and returns something like this:

class NameOfTheClass
   function Method1($arg1, $arg2, $arg2)
   private function Method2($arg1, $arg2, $arg2)
   public function Method2($arg1, $arg2, $arg2)

abstract class AnotherClass
   function Method1($arg1, $arg2, $arg2)
   private function Method2($arg1, $arg2, $arg2)
   public function Method2($arg1, $arg2, $arg2)

function SomeFunction($arg1, $arg2, $arg3)

This function should return all the classes, methods and function that exist in the given file with all the defined identifiers (abstract, public, private, protected, static, extends, interfaces, ...).

My first tought was to use regular expressions to do this, however these behave quite badly with comments, ie: /* this function returns(max(salary)) */ and become quite complex if I want to properly support scopes.

Another possible solution was to use the following built-in PHP functions:

get_declared_classes
get_declared_interfaces
get_defined_functions
get_class_methods

However these functions don't allow me to see the file where the classes / methods / functions are defined and thus it's not very useful.

I believe the Tokenizer extension is the solution for my problem, however I have never used this extension before.

like image 296
Alix Axel Avatar asked Sep 12 '09 11:09

Alix Axel


People also ask

What is function argument PHP?

PHP Function ArgumentsAn argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. The following example has a function with one argument ($fname).

What is parsing in PHP?

Definition and Usage. The parse_str() function parses a query string into variables. Note: If the array parameter is not set, variables set by this function will overwrite existing variables of the same name. Note: The magic_quotes_gpc setting in the php.

What is the class in PHP?

A PHP class, and more generally, object-oriented programming, provides additional approaches to reusability, and can be used for a variety of purposes: They can describe entities that have known properties and behaviors. They can be used as messages to functions and other objects.

How do you call a function outside the class in PHP?

you can use require_once() with exact path of that class after that you should use the object of the included class and function name for calling the function of outer class. Save this answer.


2 Answers

If you are using PHP 5, the Reflection API is your tool.

Example:

$class = new ReflectionClass("NameOfTheClass");
$methods = $class->getMethods();
foreach($methods as $m) {
    print $m->name;
    $m->isPrivate() ? print "Private" : print "";
    $m->isPublic() ? print "Public" : print "";
    $params = $m->getParameters();
    foreach($params as $p) {
        print $p->getName();
        }
}
like image 96
Davide Gualano Avatar answered Sep 24 '22 10:09

Davide Gualano


I suggest the following procedure:

  1. store the current output of get_declared_classes, get_declared_interfaces and get_defined_functions(if you really need to support them)
  2. include the file
  3. compare get_declared_classes, get_declared_interfaces and get_defined_functions with the ones you stored to see what's new
  4. use reflection to analyze them
  5. goto step 2 for the next file
like image 20
André Hoffmann Avatar answered Sep 22 '22 10:09

André Hoffmann