5

I have been beating my head for a couple hours trying to figure out why the autoload is not working for "Authentication\auth()". The "dBase\db()" class is loading just fine, but i'm getting:

Error: Class 'Authentication\auth' not found in /var/htdocs/dev/test.php on line 8

when calling test.php.

file_structure

Root composer.json -

  "require": {
    "geeshoe/dbClass": "dev-develop",
    "geeshoe/authClass": "dev-master"
  },
  "autoload": {
    "psr-4": {
      "dBase\\": "vendor/geeshoe/dbclass/",
      "Authentication\\": "vendor/geeshoe/authClass/"
    }
  }

authClass.php header -

<?php
namespace Authentication;

use dBase\db;

class auth extends db
{

test.php -

if (file_exists("vendor/autoload.php")) {
    require "vendor/autoload.php";
} else {
    echo "Dam.. Something went wrong!";
}
$test = new \dBase\db();
$var = new \Authentication\auth();

If someone could point out the obvious to me, that would be great. On a side note, autoload is not specified in the authClass->composer.json file for testing purposes.

Jaxchief
  • 146
  • 3
  • 10
  • 1. Anything in `vendor/` is created/controlled by composer and should have its own autoloading config defined already. Don't add entries for them in `composer.json`. 2. Do not create or modify anything inside `vendor/`, only composer itself should ever touch this folder and anything in it. Your code should live somewhere *else* in the project folder, eg: `src/` or `lib/`. – Sammitch Dec 19 '17 at 02:04
  • @Sammitch Thank you for the information. Both of these classes are in development and not production ready, hence not having autoload configured in their respective composer.json files.. – Jaxchief Dec 19 '17 at 02:39

1 Answers1

8

The problem here is that in fact you don't use PSR-4. In PSR-4 class name should match file name. For db class its fine because db class is located in db.php file, but auth class is located in authClass.php file and that's the problem. You should update file name to auth.php

You might need to run:

composer dump-autoload

Also keep in mind in real packages, one vendor package has one namespace, so you don't create multiple namespaces for single package but only single

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291