Parsing Classes, Functions and Arguments in PHP

懵懂的女人 提交于 2019-12-04 06:32:15

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();
        }
}

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 you found out yourself, regex are quite not the right tool for the job, here ^^

And, like you said, the built-in functions you proposed are not that helpful either -- only thing that might be helpful is that they allow you to know which class exists... But they'll return builtin classes too :-(

Using the Tokenizer extension seems a bit overkill/hard to me ; I would probably not go that way, actually : too "low-level", I suppose.


Instead, I would take a look at PHP's Reflection API : it exists exactly to reverse-engineer classes, interfaces, functions, ...

So, I suppose it would be quite well-suited for what you are trying to do.


Edit : here is a quick example :

First, let's try to do reflection on a class :

include dirname(__FILE__) . '/temp-2.php';
$rC = new ReflectionClass('MyFirstClass');

You can now find out in which file it was declared, and which methods are in it :

var_dump($rC->getFileName());
var_dump($rC->getMethods());

Which will get you :

string '/home/squale/developpement/tests/temp/temp-2.php' (length=48)

array
  0 => &
    object(ReflectionMethod)[2]
      public 'name' => string '__construct' (length=11)
      public 'class' => string 'MyFirstClass' (length=12)
  1 => &
    object(ReflectionMethod)[3]
      public 'name' => string 'glop' (length=4)
      public 'class' => string 'MyFirstClass' (length=12)


And now, to get informations on each method :

foreach ($rC->getMethods() as $rM) {
    var_dump($rM, $rM->getParameters());
    echo '-----';
}

You'll get :

object(ReflectionMethod)[3]
  public 'name' => string '__construct' (length=11)
  public 'class' => string 'MyFirstClass' (length=12)

array
  0 => &
    object(ReflectionParameter)[4]
      public 'name' => string 'arg1' (length=4)
  1 => &
    object(ReflectionParameter)[5]
      public 'name' => string 'arg2' (length=4)

-----

object(ReflectionMethod)[2]
  public 'name' => string 'glop' (length=4)
  public 'class' => string 'MyFirstClass' (length=12)

array
  0 => &
    object(ReflectionParameter)[5]
      public 'name' => string 'a' (length=1)


From there, you should be able to dig a bit more ; and arrive to what you first asked ;-)


As a sidenote : there is one thing I have no idea about is : "how to find which classes / methods are declared in a given file" :-(

If anyone has an idea, it'll be welcome !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!