We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Unexpected behavior of Phalcon\Loader with Namespaces (substrings)

Hi everyone.

I'm having a problem in my project, and I'm not sure if it is because something I did or not. I used Phalcon\Loader to register a namespace - App.

$loader = new \Phalcon\Loader();
$namespaces = array() ;
$namespaces["App"] = realpath(__DIR__ ."/../App/") ;
$loader->registerNamespaces( $namespaces ) ;
$loader->register();

Aftewards, i created classes under this namespace (following PSR-4 standards). For example :

  • App
    • Test1
      • Class1. php (class Class1 with namespace App\Test1)
    • Test2
      • Class1.php (class Class1 with namespace App\Test2)

It all works really nicely, but if I happen to use a namespace containing the registered one - for example:

  • App
    • TestingApp
      • Class2.php (class Class2 with namespace App\TestingApp)

I get a fatal error of class not found. The loader doesn't seem to find the Class2 class when I use the fully qualified name (App\TestingApp\Class2).

Did someone else came across this type of problem?

Thanks!



5.7k

Is it safe to assume that you've double checked the namespaces in your App/TestingApp/Class2.php file?

I had this issue once but it was because, in relation to your example, I created Class2.php as a copy of Class1.php and forgot to update the namespace :-/

Other than that, I haven't come acorss this issue in Phalcon 1.3.x or 2.0.x.

Hey Steven, thanks for the reply.

One would think there was some copy-past problem, right? But I have checked and it is not the case...

If I change the 'TestingApp' to 'TestingApp1' (directory name and namespace) it works fine... And it happens with Phalcon 1.3.x and 2.0.1

Is it safe to assume that you've double checked the namespaces in your App/TestingApp/Class2.php file?

I had this issue once but it was because, in relation to your example, I created Class2.php as a copy of Class1.php and forgot to update the namespace :-/

Other than that, I haven't come acorss this issue in Phalcon 1.3.x or 2.0.x.