Get class name from file

后端 未结 10 1832
轮回少年
轮回少年 2020-12-08 10:29

I have a php file which contains only one class. how can I know what class is there by knowing the filename? I know I can do something with regexp matching but is there a st

相关标签:
10条回答
  • 2020-12-08 11:15
    $st = get_declared_classes();
    include "classes.php"; //one or more classes in file, contains class class1, class2, etc...
    
    $res = array_values(array_diff_key(get_declared_classes(),$st));
    print_r($res); # Array ([0] => class1 [1] => class2 [2] ...)
    
    0 讨论(0)
  • 2020-12-08 11:16

    I modified Nette\Reflection\AnnotationsParser that so it returns an array of namespace+classname that are defined in the file

    $parser = new PhpParser();
    $parser->extractPhpClasses('src/Path/To/File.php');
    
    
    class PhpParser
    {
        public function extractPhpClasses(string $path)
        {
            $code = file_get_contents($path);
            $tokens = @token_get_all($code);
            $namespace = $class = $classLevel = $level = NULL;
            $classes = [];
            while (list(, $token) = each($tokens)) {
                switch (is_array($token) ? $token[0] : $token) {
                    case T_NAMESPACE:
                        $namespace = ltrim($this->fetch($tokens, [T_STRING, T_NS_SEPARATOR]) . '\\', '\\');
                        break;
                    case T_CLASS:
                    case T_INTERFACE:
                        if ($name = $this->fetch($tokens, T_STRING)) {
                            $classes[] = $namespace . $name;
                        }
                        break;
                }
            }
            return $classes;
        }
    
        private function fetch(&$tokens, $take)
        {
            $res = NULL;
            while ($token = current($tokens)) {
                list($token, $s) = is_array($token) ? $token : [$token, $token];
                if (in_array($token, (array) $take, TRUE)) {
                    $res .= $s;
                } elseif (!in_array($token, [T_DOC_COMMENT, T_WHITESPACE, T_COMMENT], TRUE)) {
                    break;
                }
                next($tokens);
            }
            return $res;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 11:19

    You may be able to use the autoload function.

    function __autoload($class_name) {
        include "special_directory/" .$class_name . '.php';
    }
    

    And you can echo $class_name. But that requires a directory with a single file.

    But it is standard practice to have one class in each file in PHP. So Main.class.php will contain Main class. You may able to use that standard if you are the one coding.

    0 讨论(0)
  • 2020-12-08 11:20

    I spent lots of productive time looking for a way around this. From @netcoder's solution its obvious there are lots of cons in all the solutions so far.

    So I decided to do this instead. Since most PHP classes has class name same as filename, we could get the class name from the filename. Depending on your project you could also have a naming convention. NB: This assume class does not have namespace

    <?php
        $path = '/path/to/a/class/file.php';
    
        include $path;
    
        /*get filename without extension which is the classname*/
        $classname = pathinfo(basename($path), PATHINFO_FILENAME);
    
        /* you can do all this*/
        $classObj = new $classname();
    
        /*dough the file name is classname you can still*/
        get_class($classObj); //still return classname
    
    0 讨论(0)
提交回复
热议问题