Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer autoload - Class not found exception

I am pulling my hairs out on this one, and that does not happen frequently.

I am trying to use composers autoloader, with a directory of mine. The autoloader works perfectly with another directory.

I am using the following filestructure

-Root
 | application
   | Module
      | Users
         | Users.php

The Users.php contains the following code

<?php
namespace Module\Users;

class Users {
  public function test() { return "Testing hippie-yaay!"; }
}

The composer.json contains the following psr-0 autoload

"psr-0":{"Module\\": "application/"}

which in the autoloader_namespaces.php compiles to the following

'Module\\' => array($baseDir . '/application'),

Which then again, is totally correct (Ive tested this by echoing out the basedir -application string. It is totally correct.

now. In my main class i do the following

<?php
namespace System\Core;
use Module\Users\Users;

    class Initiater {
      public function bootSystem() {
        $u = new Users();
      }
    }

(this is basically what i do, ofcourse the other psr-0 autoloads i were talking about at the top are over this one, working just fine.)

I then get the following error.

Fatal error: Class 'Module\Users\Users' not found in

FYI: I tried with just "use Module\Users;" and "new \Module\Users\Users();" both returning the same error.

I hope one of you knows whats going on here. Best regards. Jonas

like image 909
Jonas m Avatar asked Feb 14 '26 10:02

Jonas m


2 Answers

new Users\Users();

is probably what you're looking for

like image 124
Sven Avatar answered Feb 15 '26 23:02

Sven


I had the same issue. I did a "composer update" and it fixed my issue. For some reason I had a namespace mismatch in the autoload_classmap.php

'Module' => $baseDir . '/application/Module/Users/Users.php',

and it was missing some info about the key part:

'Module\\Users' => $baseDir . '/application/Module/Users/Users.php',

So, like other mentioned, it's probably due to some invalid data that doesn't match your code.

Sometime you make a modification (like fixing a typo) and forgot to regenerate the autoloader and everything seems nice.

like image 26
Sauleil Avatar answered Feb 16 '26 00:02

Sauleil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!