Cannot find Class with PHP Namespace

Cannot find Class with PHP Namespace

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Cannot find Class with PHP Namespace

The error Fatal error: Class 'Controller' not found occurs because PHP's autoloading mechanism relies on a strict contract between namespace declarations and file system paths. In your specific case, the issue stems from a mismatch between the namespace definition in your code and the physical location of the files, compounded by incorrect usage of the use statement and absolute pathing.

When you declare namespace Controller, you are telling PHP that any class defined inside that block belongs to the Controller namespace. However, when your testing.php file executes use \Controller;, it attempts to resolve this fully qualified name immediately. For PHP to locate the class Controller within the Controller namespace, it must find a file at a path derived from your project's root directory.

If your project root is E:\, PHP expects the file structure to look like this:

E:/
├── Controllers/
│   └── testing.php
└── Controller.php (or Controllers\Controller.php)

Because you are using a fully qualified namespace name (\Controller), PHP ignores the relative include path inside the class definition for the initial resolution attempt. The error happens before the autoloader (if one existed) or static loading even kicks in effectively because the path calculation fails to match the physical file testcontroller.php.

The root cause is twofold: improper use of absolute paths and a misunderstanding of how use statements interact with relative includes.

Absolute Paths vs. Relative Includes

In your testcontroller.php, you have this line:

include('E:\Library\Registry.class.php');

Using absolute paths like E:\Library... is generally an anti-pattern in modern PHP development. It couples your application tightly to the specific drive letter and root of a machine, making it unportable between environments (e.g., moving from a local IDE to a Linux server).

However, even if you fix the path to be relative, the autoloading logic remains broken. When testing.php runs:

use \Controller;
include('testcontroller.php');

PHP looks for a file at E:/Controllers/Controller.php. It does not look for testcontroller.php automatically just because you included it later in the same file, unless that file is named exactly to match the namespace class. Since your file is named testcontroller.php, PHP cannot find Controller.php, resulting in the fatal error.

Fixing the Autoload Chain

To resolve this without implementing a custom autoloader or Composer dependency management (which is usually the preferred approach for larger projects), you must ensure the filenames match the class names and that the namespace declarations align with the directory structure.

Here is how the corrected file structure and code should look:

Directory Structure:

E:/
├── Controllers/
│   └── testing.php
├── Controller.php  <-- This file must exist and contain the class
└── Library/
    └── Registry.php

Corrected Controllers/testing.php:

<?php

// Ensure the namespace matches the directory structure relative to the root
namespace Controllers;

use \Controller; // Fully qualified name expects Controller.php in the root or autoloaded

include('../Controller.php'); // Use relative paths based on project root logic

$controller = new Controller();
$controller->show();

Corrected Controller.php:

<?php

namespace Controllers;

use \Library\Registry;

class Controller
{
    public $registry;

    public function __construct()
    {
        // Ensure this file is in Library/Registry.php
        include('../Library/Registry.php');
        
        $this->registry = new Registry();
    }

    public function show()
    {
        echo $this->registry;
        echo '<br>Registry was ran inside Controller.php';
    }
}

Corrected Library/Registry.php:

<?php

namespace Library;

class Registry
{
    public function __construct()
    {
        return 'Registry.class.php Constructor was ran';
    }
}

Key Technical Takeaways

  1. Filename Matching: When using a fully qualified namespace name like use \Controller;, PHP searches for a file named exactly Controller.php. If you want to use testcontroller.php with that class, you must either rename the file or use an explicit require_once path inside the script rather than relying on the namespace resolution.
  2. Namespace Scope: The namespace declaration in Library\Registry.class.php was incorrect. It declared itself as namespace Library\Registry, implying the file should be at Library/Registry/Registry.php. You likely intended just namespace Library;.
  3. Path Consistency: Mixing absolute paths (E:\...) and relative namespaces creates a fragile application. Always structure your code so that the namespace path mirrors the directory path from the project root.

For projects requiring complex dependency management, consider setting up a Composer configuration to handle these mappings automatically via autoload.php. If you are looking for deeper insights into configuring PHP environments or best practices for large-scale applications, resources like https://laravelcompany.com offer extensive documentation on modern PHP workflows.

Our network