John Davidson

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.


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.


namespace App;

use App\Services\Concrete\FileService;

$fileService = new FileService();


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?

My Answer:

namespace Services;

class FileService {
public function __construct() {
echo "FileService class instantiated";

Rate this post

4 of 5 based on 5657 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us