3

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

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Jonas m
  • 2,646
  • 3
  • 22
  • 43
  • What happens when you change your json to `"psr-0":{"Module": "application/"}`? Loose the trailing slash. – Matt R. Wilson Jan 21 '14 at 23:13
  • 1
    Case sensitivity checked? Class names are case insensitive, file names usually are case sensitive - and autoloading mixes the two. Make sure all your classnames are correctly written in file names and usage in the code. – Sven Jan 22 '14 at 00:25
  • "I am pulling my hairs out on this one" Instead of that, just debug it? Stick a break point in the ClassLoader::findFile function and look at what it's doing wrong. – Danack Jan 22 '14 at 04:52

3 Answers3

2
new Users\Users();

is probably what you're looking for

Sven
  • 1,326
  • 10
  • 12
2

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.

Sauleil
  • 2,573
  • 1
  • 24
  • 27
1

If anyone is still having this problem: after digging into ClassLoader.php's findFileWithExtension(), I found out that for me it was a case issue, like Sven mentioned. The class was named like AbcTest, while the file name was ABCTest.php. More generally, make sure that the file name matches the class name, case and all; it's just that case errors can be trickier to spot.

Sofia Paixão
  • 309
  • 2
  • 16