A
A
AlpineMilk2018-03-31 20:46:56
PHP
AlpineMilk, 2018-03-31 20:46:56

Why doesn't it find the namespace class?

there is an index.php file and Parser.php is on the same level with it,
here is the index code

require_once "vendor/autoload.php";

use src\Parser;

$parser = new Parser();

parser class has namespace src;
this connection throws an error
Fatal error: Uncaught Error: Class 'src\Parser' not found

why doesn't he find it? I also tried to put the class in the src folder, I thought maybe he was looking for the folders, but also without success, here is the composer.json if needed,
{
    "require": {
        "phpoffice/phpspreadsheet": "^1.2"
    },
    "autoload": {
    	"psr-4": {"excel.local": "/"}
    }
}

yes, and how to register autoloading directly and the root of the directory? did i do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2018-03-31
@Sanasol

there is an index.php file and Parser.php is on the same level with it
Then it should lie in src/Parser.php, and not at the same level.
https://www.php-fig.org/psr/psr-4/

P
profaller, 2018-04-01
@profaller

AlpineMilk , write in the composer config how the namespace is called, which looks at the root, and rebuild autoload.
More or less like this:

"autoload": {
    "psr-4": {
      "rootnamespace\\": "/"
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question