• <bdo id='tc6Ru'></bdo><ul id='tc6Ru'></ul>
  1. <tfoot id='tc6Ru'></tfoot>

  2. <small id='tc6Ru'></small><noframes id='tc6Ru'>

    <legend id='tc6Ru'><style id='tc6Ru'><dir id='tc6Ru'><q id='tc6Ru'></q></dir></style></legend>

      <i id='tc6Ru'><tr id='tc6Ru'><dt id='tc6Ru'><q id='tc6Ru'><span id='tc6Ru'><b id='tc6Ru'><form id='tc6Ru'><ins id='tc6Ru'></ins><ul id='tc6Ru'></ul><sub id='tc6Ru'></sub></form><legend id='tc6Ru'></legend><bdo id='tc6Ru'><pre id='tc6Ru'><center id='tc6Ru'></center></pre></bdo></b><th id='tc6Ru'></th></span></q></dt></tr></i><div id='tc6Ru'><tfoot id='tc6Ru'></tfoot><dl id='tc6Ru'><fieldset id='tc6Ru'></fieldset></dl></div>

      如何获取特定命名空间内的所有类名?

      How to get all class names inside a particular namespace?(如何获取特定命名空间内的所有类名?)
        <i id='lmobp'><tr id='lmobp'><dt id='lmobp'><q id='lmobp'><span id='lmobp'><b id='lmobp'><form id='lmobp'><ins id='lmobp'></ins><ul id='lmobp'></ul><sub id='lmobp'></sub></form><legend id='lmobp'></legend><bdo id='lmobp'><pre id='lmobp'><center id='lmobp'></center></pre></bdo></b><th id='lmobp'></th></span></q></dt></tr></i><div id='lmobp'><tfoot id='lmobp'></tfoot><dl id='lmobp'><fieldset id='lmobp'></fieldset></dl></div>
            <bdo id='lmobp'></bdo><ul id='lmobp'></ul>
          • <small id='lmobp'></small><noframes id='lmobp'>

            <tfoot id='lmobp'></tfoot>
                  <tbody id='lmobp'></tbody>

              1. <legend id='lmobp'><style id='lmobp'><dir id='lmobp'><q id='lmobp'></q></dir></style></legend>

                本文介绍了如何获取特定命名空间内的所有类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想获取命名空间中的所有类.我有这样的事情:

                I want to get all classes inside a namespace. I have something like this:

                #File: MyClass1.php
                namespace MyNamespace;
                
                class MyClass1() { ... }
                
                #File: MyClass2.php
                namespace MyNamespace;
                
                class MyClass2() { ... }
                
                #Any number of files and classes with MyNamespace may be specified.
                
                #File: ClassHandler.php
                namespace SomethingElse;
                use MyNamespace as Classes;
                
                class ClassHandler {
                    public function getAllClasses() {
                        // Here I want every classes declared inside MyNamespace.
                    }
                }
                

                我在 getAllClasses() 中尝试了 get_declared_classes()MyClass1MyClass2 不在列表中.

                I tried get_declared_classes() inside getAllClasses() but MyClass1 and MyClass2 were not in the list.

                我该怎么做?

                推荐答案

                通用的方法是获取项目中所有完全限定的类名(具有完整命名空间的类),然后按所需的命名空间进行过滤.

                The generic approach would be to get all fully qualified classnames (class with full namespace) in your project, and then filter by the wanted namespace.

                PHP 提供了一些本机函数来获取这些类(get_declared_classes 等),但它们无法找到尚未加载的类(包括/要求),因此它不能按预期使用自动加载器(例如作曲家).这是一个主要问题,因为自动加载器的使用非常普遍.

                PHP offers some native functions to get those classes (get_declared_classes, etc), but they won't be able to find classes that have not been loaded (include / require), therefore it won't work as expected with autoloaders (like Composer for example). This is a major issue as the usage of autoloaders is very common.

                所以你最后的办法是自己查找所有 PHP 文件并解析它们以提取它们的命名空间和类:

                So your last resort is to find all PHP files by yourself and parse them to extract their namespace and class:

                $path = __DIR__;
                $fqcns = array();
                
                $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
                $phpFiles = new RegexIterator($allFiles, '/.php$/');
                foreach ($phpFiles as $phpFile) {
                    $content = file_get_contents($phpFile->getRealPath());
                    $tokens = token_get_all($content);
                    $namespace = '';
                    for ($index = 0; isset($tokens[$index]); $index++) {
                        if (!isset($tokens[$index][0])) {
                            continue;
                        }
                        if (T_NAMESPACE === $tokens[$index][0]) {
                            $index += 2; // Skip namespace keyword and whitespace
                            while (isset($tokens[$index]) && is_array($tokens[$index])) {
                                $namespace .= $tokens[$index++][1];
                            }
                        }
                        if (T_CLASS === $tokens[$index][0] && T_WHITESPACE === $tokens[$index + 1][0] && T_STRING === $tokens[$index + 2][0]) {
                            $index += 2; // Skip class keyword and whitespace
                            $fqcns[] = $namespace.'\'.$tokens[$index][1];
                
                            # break if you have one class per file (psr-4 compliant)
                            # otherwise you'll need to handle class constants (Foo::class)
                            break;
                        }
                    }
                }
                

                如果您遵循 PSR 0 或 PSR 4 标准(您的目录树反映了您的命名空间),则无需过滤任何内容:只需提供与您想要的命名空间对应的路径即可.

                If you follow PSR 0 or PSR 4 standards (your directory tree reflects your namespace), you don't have to filter anything: just give the path that corresponds to the namespace you want.

                如果你不喜欢复制/粘贴上面的代码片段,你可以简单地安装这个库:https://github.com/gnugat/nomo-spaco.如果您使用 PHP >= 5.5,还可以使用以下库:https://github.com/hanneskod/classtools .

                If you're not a fan of copying/pasting the above code snippets, you can simply install this library: https://github.com/gnugat/nomo-spaco . If you use PHP >= 5.5, you can also use the following library: https://github.com/hanneskod/classtools .

                这篇关于如何获取特定命名空间内的所有类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How do I parse XML containing custom namespaces using SimpleXML?(如何使用 SimpleXML 解析包含自定义命名空间的 XML?)
                SimpleXML SOAP response Namespace issues(SimpleXML SOAP 响应命名空间问题)
                Problems with PHP namespaces and built-in classes, how to fix?(PHP 命名空间和内置类的问题,如何解决?)
                Use php namespace inside function(在函数内部使用 php 命名空间)
                unexpected #39;use#39; (T_USE) when trying to use composer(尝试使用作曲家时意外的“使用(T_USE))
                PHP adding custom namespace using autoloader from composer(PHP使用来自作曲家的自动加载器添加自定义命名空间)
                  1. <tfoot id='ur4Ef'></tfoot>
                    <i id='ur4Ef'><tr id='ur4Ef'><dt id='ur4Ef'><q id='ur4Ef'><span id='ur4Ef'><b id='ur4Ef'><form id='ur4Ef'><ins id='ur4Ef'></ins><ul id='ur4Ef'></ul><sub id='ur4Ef'></sub></form><legend id='ur4Ef'></legend><bdo id='ur4Ef'><pre id='ur4Ef'><center id='ur4Ef'></center></pre></bdo></b><th id='ur4Ef'></th></span></q></dt></tr></i><div id='ur4Ef'><tfoot id='ur4Ef'></tfoot><dl id='ur4Ef'><fieldset id='ur4Ef'></fieldset></dl></div>
                    <legend id='ur4Ef'><style id='ur4Ef'><dir id='ur4Ef'><q id='ur4Ef'></q></dir></style></legend>
                  2. <small id='ur4Ef'></small><noframes id='ur4Ef'>

                      <bdo id='ur4Ef'></bdo><ul id='ur4Ef'></ul>

                            <tbody id='ur4Ef'></tbody>