Question

I have a problem with a Symfony2 ClassLoader Component.

Start code:

    require_once("vendor/autoload.php");

    use Symfony\Component\ClassLoader\UniversalClassLoader;

    $loader = new UniversalClassLoader();

    $loader->registerNamespace('MyClass', 'src');
    $loader->useIncludePath(true);
    $loader->register();

My folder structure:

-src
--->MyClass
------->MyClass1.php (MyClass\MyClass1)
------->MyClass2.php (MyClass\MyClass2)

If I try to include a class, the class is not loaded, for example:

use MyClass\SimpleClass1;
$SimpleClass1 = new MyClass\SimpleClass1\Class1();

but if I use the method loadClass() working:

$loader->loadClass("MyClass\\SimpleClass1");
use MyClass\SimpleClass1;
$SimpleClass1 = new MyClass\SimpleClass1\Class1();
Was it helpful?

Solution

In your first example, you use MyClass\SimpleClass1\Class1. The configured path for that class is src/MyClass/SimpleClass1/Class1.php, which does not exists. (btw, the use statement is useless there)

Your second example shows MyClass\SimpleClass1, which is -as you can see- different from your first example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top