Symfony 4, из метода в пользовательском классе foo (src / Utils / foo.php), require_one не имеет никакого эффекта - PullRequest
0 голосов
/ 17 октября 2018

Мне нужно загрузить все классы, которые находятся в определенной директории проекта ("dir1 / dir2") для списка всех методов (и только список всех методов, но не использовать их)

Я могу перечислить все классы, ноЯ не могу загрузить их и не понимаю почему!

Пока у меня есть только класс faa в /dir1/dir2/faa.php с некоторыми методами.

И в пользовательском классе 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 !
            }
        }
    }
}

Когда я использую свой класс Foo из контроллера Symfony, как это:

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

Я не могуперечислите все методы моего класса Faa, потому что функция class_exists возвращает false: /

Где моя ошибка?Спасибо за помощь :)

...