简体   繁体   English

使用反射获取类名和方法名

[英]use reflection get class name and method name

In my website there is folder named as plugins. 在我的网站上有一个名为plugins的文件夹。 I want to get all class used in that folder and method of each class. 我想获取该文件夹中使用的所有类以及每个类的方法。 But I don't know how can I use reflection to get method name and class name from that folder? 但是我不知道如何使用反射从该文件夹中获取方法名称和类名称? Where should I write code for it? 我应该在哪里编写代码?

Method 1 : you may use autoloader to load classes, you can collect all classes in an iteratable array if resource count indefinite. 方法1:您可以使用自动加载器加载类,如果资源计数不确定,则可以将所有类收集到可迭代数组中。

Method 2 : you may use get_declared_classes() method to get all declared classes and match with regex which are point your plugin. 方法2:您可以使用get_declared_classes()方法获取所有已声明的类,并与正则表达式匹配,它们是您的插件指向的。

after all of it use reflection. 毕竟使用反射。

<?php
    foreach ($declaredClasses as $key => $classWithNamespace) {
        $class = new ReflectionClass($classWithNamespace);
        $methods = $class->getMethods();
        foreach ($methods as $method) {
           //analyze your methods
        }

}

But if you don't cache this things in your prod env. 但是,如果您不将此内容缓存在产品环境中。 There would be many performance issues, reflection is not a cheap cost! 将会有很多性能问题,反射并不是不菲的代价!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM