首页 > 解决方案 > PHP,来自 PSR-4 项目,使用相同的命名空间打印所有类的所有方法

问题描述

从使用 psr-4 约定的项目(symfony 项目)中,我想(只是)打印具有特定命名空间的所有类的所有方法(这些类位于同一目录中)

我对这里的文档进行了一些尝试https://getcomposer.org/doc/00-intro.md但我失败了并且不太了解这个库是如何工作的(也许我的英语能力很差?:/)

有人可以帮助我吗?

我最后一次尝试:

class myUtils{

    public function getAllMethods(){

        $loader = new ClassLoader();
        $loader->loadClass('App\DIR1');
        $loader->register();

        // Faa and Foo are the name of the classes of the file Fii.php and Faa.php located in DIR1 directory, the namespace of theses classes is App\DIR1
        if (class_exists("Foo") && class_exists("Faa")){
            dump('Its OK !');
        }else{
            dump('its NOT OK');
        }

        // the dump "Its NOT OK" is printed, I don't find how I can load dynamically Faa and Foo classes..
    }
}

标签: phpsymfonycomposer-phppsr-4

解决方案


只是玩,ClassLoader::getClassMap()你没有其他办法,因为只有当你尝试使用它时才会加载类。您必须通过自定义函数过滤类。

无论如何,如果您没有预定义的类列表,那么解决方案将是肮脏的。

对于使用反射的方法,这很容易。


推荐阅读