简体   繁体   中英

PHP Namespaces with fatal error

I'm developing a project but I can't seem to get the namespaces to work.

I have the following structure:

src folder:

- Searcher.php
- execSearch.php

In the Searcher.php file ( that represents the Searcher class ) I have the following line:

namespace search\Searcher;

and in execSearch.php I try to use the namespace like this:

use search\Searcher\Searcher;

I also tried.

use search\Searcher;

but with no success, I always get the same error:

Fatal Error: Class 'search\Searcher\Searcher' not found in searchExec.php

I also defined in my composer.json file the following:

    "autoload": {
    "psr-4": {
        "search\\Searcher\\": "src"
    }
}

How can use the namespaces correctly so I can use the class in execSearch.php?

With PSR-4 you must place your search\\Searcher\\Searcher class in the file src\\search\\Searcher\\Searcher.php .

So your tree looks like this:

composer.json
src/
   execSearch.php    (or searchExec.php?)
   search/
     Searcher/
       Searcher.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM