1

I am using the ps-4 autoloader using Composer.

"autoload": {
    "psr-4": {
        "App\\":"app/",
        "Database\\":"database/"
    }
},

So, I have the main index.php file in the root directory like this

require 'vendor/autoload.php';
use App\Server;
$server = new Server();

The root directory has the app folder and a class called Server in it like this

namespace App;
echo "in server<hr>";

class Server{}

I get the echo "in server" so the class file gets included. But I get this error

Fatal error: Uncaught Error: Class "App\Server" not found in /var/www/html/index.php:8 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 8

It looks for a class called "App\Server" instead of "Server". How do I fix this?

Saeesh Tendulkar
  • 638
  • 12
  • 30
  • Should it be `namespace App`? – Nigel Ren May 01 '22 at 15:18
  • @NigelRen Yep Correct. I fixed that. But it still gives the same error – Saeesh Tendulkar May 01 '22 at 15:22
  • Please supply a [mcve] - a `use` statement on its own doesn't trigger the autoloader, nor look for the class (it's just a directive to the compiler to do some string replacement), so will not produce that error. – IMSoP May 01 '22 at 15:25
  • @IMSoP Sorry. I did not get what you want exactly. I have changed the "Server.php" file to say namespace App as you said in your answer. But it still gives the same error – Saeesh Tendulkar May 01 '22 at 15:27
  • What I'm asking for is the code that actually generates the error you're getting - the code you've shown cannot generate that error, because it never looks for the "App\Server" class. The code will run just fine with no classes defined at all: https://3v4l.org/inWE8 I understand if you don't want to show us your real application, but you need to show us code that you've actually tested. – IMSoP May 01 '22 at 15:32
  • Sorry if I was misleading but the line "use App\Server;" does not generate the error. Its when I instantiate the class by "new Server()", the class file gets included, but it looks for class "App\Server" and gives the error in my updated question – Saeesh Tendulkar May 01 '22 at 15:38
  • Is the path to the autoload file correct in `require 'vendor/autoload.php';` ? – nice_dev May 01 '22 at 15:41
  • @nice_dev Yes. The vendor folder is in the root directory, same as index.php – Saeesh Tendulkar May 01 '22 at 15:44
  • @SaeeshTendulkar Ok, but this error is unlikely to happen or at least not reproducible at our end. – nice_dev May 01 '22 at 15:53
  • @nice_dev I am shocked too but here's the live example http://54.173.27.246/ – Saeesh Tendulkar May 01 '22 at 15:57
  • @SaeeshTendulkar Ok, but you haven't shown in your question as to in which file you have declared `new Server()` ? – nice_dev May 01 '22 at 15:59
  • 1
    @nice_dev Damn. Sorry for the confusion. I have update it now. Its in the index.php file – Saeesh Tendulkar May 01 '22 at 16:02
  • @SaeeshTendulkar What does `get_declared_classes()` return after the autoload code? Do you see your `Server` getting loaded? – nice_dev May 01 '22 at 16:07
  • @nice_dev It does not show Server in get_declared_classes(). You can check the link to see it – Saeesh Tendulkar May 01 '22 at 16:10
  • @SaeeshTendulkar Ok, can you just do `composer dump-autoload` and try again. – nice_dev May 01 '22 at 16:14
  • @nice_dev Done. I get the same error. Updated the code in the link – Saeesh Tendulkar May 01 '22 at 16:16
  • @SaeeshTendulkar Ok, but I am afraid I can't be of help any further. – nice_dev May 01 '22 at 16:27

0 Answers0