Search code examples
phpclasssymfonynamespacesautoloader

Symfonys Autoloader - Implement Parent Namespaces


I am using Symfonys Autoloader for a project having the following folder/class structure:

App
+- Package1
|
+- Package2
  +- Class1.php
|
- Interface1.php

How do i implement a class from a parent namespace now. Like Interface1 from Class1 for example. This does not work:

namespace App\Package1

Class1 implements App\Interface1
{
    //implement some functions here...
}

The Autoloader trys to include App\Package2\Class1\App\Interface1 then.

Best regards,

Manuel


Solution

  • the symfony's classloader works great:)

    In this case, it's a php syntax problem.

    You need to import the namespace with "use" or more easily you just need a backslash in front of the classname

    for example:

    namespace App\Package1;
    
    use App\Interface1;
    
    Class1 implements Interface1
    {
        //implement some functions here...
    }
    

    or

    namespace App\Package1;
    
    Class1 implements \App\Interface1;
    {
        //implement some functions here...
    }