Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer PSR-4 Autoload class not found

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.

like image 578
Jaxchief Avatar asked Mar 08 '23 08:03

Jaxchief


1 Answers

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

like image 155
Marcin Nabiałek Avatar answered Mar 16 '23 11:03

Marcin Nabiałek