Home > Back-end >  ClassNotFoundException (PHP)
ClassNotFoundException (PHP)

Time:03-06

My folder structure looks like this:

src/
  Creh/
    Main.php
    Json.php
    Ini.php

This is what my composer.json looks like:

"autoload": {
    "psr-4": {
        "Uga\\": "src/"
    }
}

This is my Main.php:

<?php

namespace Uga\Creh;

require __DIR__.'../../../vendor/autoload.php';

use Creh\Ini; 

$ini = new Ini();
$ini->dump();

I am getting the following error on execution of the above code: PHP Fatal error: Uncaught Error: Class 'Creh\Ini' not found in [..]

What am I doing wrong? Any help would be much appreciated!

CodePudding user response:

I think you have the namespace set incorrectly in the Ini.php file.

It should look like this

Your Ini.php

<?php
namespace Uga\Creh;

class Ini
{
  function dump(){
      echo 1;
  }
}

Your Main.php

<?php

namespace Uga\Creh;
 
require __DIR__.'../../../vendor/autoload.php';

use Uga\Creh\Ini;

$ini = new Ini();
$ini->dump();

Don't forget to update autoload files just in case

composer dump-autoload
  • Related