'PHP namespace is not working: Class Not Found
I am building web application using pure PHP without using any frameworks. I am trying to use namespace to organise the files and classes. But when I import the classes or files using namespace and execute the php file, it is throwing error basically saying that the class or file under the namespace does not exist.
This is my file structure.
As you can see there is a index.php and FileService.php files in the different folders.
This is the content of FileService.php file.
<?php
namespace App\Services\Concrete;
use App\Services\Abstraction\IFileService;
class FileService implements IFileService
{
const IMPORT_FILE_PATH = 'import_file_structure.txt';
public function importFiles()
{
// TODO: provide implementation
}
}
Now I am trying to import the FileService class into index.php file and execute the importFiles function as follow.
<?php
namespace App;
use App\Services\Concrete\FileService;
$fileService = new FileService();
$fileService->importFiles();
When I run php index.php command in the terminal, I am getting the following error.
Fatal error: Uncaught Error: Class 'App\Services\Concrete\FileService' not found in /Users/waihein/Desktop/Code/software_developer_test/backend/App/index.php:7
Stack trace:
#0 {main}
thrown in /Users/waihein/Desktop/Code/software_developer_test/backend/App/index.php on line 7
How can I fix this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

