0

In composer.json which method should be used to load my classes in.

PSR-4:

"autoload": {
    "psr-4": {
        "Pure\\": "pure/src/Pure"",
    }
}

Or a custom autoload.php file:

"autoload": {
    "files": [
      "pure/src/Pure/autoload.php"
    ]
}

Here is the autoload.php file:

spl_autoload_register(function ($class) {

    $prefix = 'Pure\\';
    $baseDir = __DIR__;      

    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }

    $relativeClass = substr($class, $len);

    $file = $baseDir. '/' .str_replace('\\', '/', $relativeClass) . '.php';

    if (file_exists($file)) {
        require $file;
    }
});
Bxx
  • 1,615
  • 1
  • 17
  • 30
  • 2
    Do you need a custom autoloader? I doubt it. Go with PSR4 and let composer handle the rest! ;-) – nietonfir Mar 15 '16 at 20:59
  • 1
    What problems is custom autoload.php solving for you? – N.B. Mar 15 '16 at 21:00
  • Does it work with PSR-4 specified in composer.json? – axiac Mar 15 '16 at 21:01
  • I guess there's no need for the custom autoloader. I noticed every package had one, so I thought it was good practice? – Bxx Mar 15 '16 at 21:04
  • Less "magic" there is, better the library. You don't need a custom autoloader, therefore your problem is solved by eliminating the issue in the first place :) – N.B. Mar 15 '16 at 21:16
  • Sorry for interfering with your two questions, but I think they are better placed in one. – Sven Mar 15 '16 at 22:33

0 Answers0