Symfony 4,来自自定义foo类(src / Utils / foo.php)中的方法,require_one无效

时间:2018-10-17 13:05:00

标签: php symfony symfony4 require-once

我需要加载特定项目目录(“ dir1 / dir2”)中的所有类,以列出所有方法(并且仅列出所有方法,而不使用它们)

我可以列出所有类,但无法加载它们,我也不明白为什么!

目前,我在/dir1/dir2/faa.php中只有类faa,其中包含一些方法。

在自定义的Utils类中,我做了:

// in <root_dir>/src/Utils/foo.php
<?php

namespace App\Utils;
use Symfony\Component\HttpKernel\KernelInterface;

class Foo
{

    public $specificCustomDir;

    public function __construct(KernelInterface $kernel)
    {
        // I store the absolute path of the directory where I want load all classes
        $this->specificCustomDir = $kernel->getProjectDir() . "/dir1/dir2";
    }

    public function getClasses(){
        foreach (glob($this->specificCustomDir . "/*.php") as $file)
        {
            dump($file); // Its OK ! I get "/home/.../mysymfoproject/dir1/dir2/faa.php" 

            require_once($file); // It seems OK ! no PHP error, so the path of the file is correct

            $class = basename($file, '.php');

            dump($class); // Its OK ! I get "Faa"

            // the path file is OK 
            // the class name is OK

            // but class_exists return false
            if (class_exists($class))
            {
                dump("test yes"); // KO because the class_exists return false and I don't understand why 
                $obj = new $class;
                foreach(get_class_methods($obj) as $method)
                {
                    // I just want show methods, not use them !
                    dump($method);
                }
            }else{
                dump("class not found ! "); // KO, this message appears !
            }
        }
    }
}

当我从这样的symfony控制器中使用Foo类时:

$foo = new Foo();
$foo->getClasses();

我无法列出我的Faa类的所有方法,因为class_exists函数返回false:/

我的错误在哪里?感谢您的帮助:)

0 个答案:

没有答案