The Autoload Trap in PHP Namespaces
You are encountering a fatal error because your autoloader logic fundamentally misunderstands how PHP namespaces resolve class names. The specific error Class 'Class1' not found occurs because the __autoload callback receives the exact string you used to instantiate the object, not a fully qualified namespace path. In your test.php, you are calling new Class1(). At runtime, PHP treats Class1 as a global class name and passes the literal string 'Class1' to your autoloader.
When your function executes require $class . '.php';, it attempts to load a file named Class1.php from the current directory or the include path. However, your actual file is located at Person/Barnes/David/Class1.php. The namespace declaration inside that file tells PHP where to look for the class once the file is included, but the autoloader never gets the chance to find it because it is looking in the wrong place entirely.
To make this work with a legacy __autoload function (deprecated since PHP 5.4 and removed in 7.0+), you must construct the full relative path based on the incoming class name. You need to strip the namespace prefix from the expected global name and reconstruct the directory structure. A more robust approach than string concatenation is using dirname(__FILE__) to ensure you are loading files relative to the script's location rather than relying on arbitrary include paths.
Here is a corrected version of your autoloader that attempts to map the simple class name to the deep namespace folder:
<?php
function __autoload($class)
{
// Determine the directory where this file resides
$baseDir = dirname(__FILE__);
// Attempt to resolve the class path by removing potential leading backslashes
$className = ltrim($class, '\\');
// Since the class is defined in 'Person\Barnes\David',
// we assume the file structure matches the namespace.
// We need to guess the directory path based on the class name if we don't know it.
// However, without knowing the full namespace context inside __autoload,
// this is extremely fragile. A better pattern for legacy autoloaders is:
$file = $baseDir . '/' . $className . '.php';
if (file_exists($file)) {
require $file;
} else {
throw new Exception("Class not found in expected location: $file");
}
}
// This still fails because PHP looks for 'Class1.php' in the current directory first.
// It does not automatically go into Person/Barnes/David unless you specify the path.
The core issue remains that new Class1() implies a global namespace scope. You are essentially trying to force a namespaced class to behave as if it were global. If you want to use namespaces with autoloading, the calling code must import the class or use its fully qualified name.
If you update your test.php to explicitly reference the namespaced path, the simple string concatenation in your original autoloader might work, but only if the file structure matches the namespace exactly:
<?php
function __autoload($class)
{
// Construct the path assuming the directory name matches the namespace segment
$file = dirname(__FILE__) . '/' . str_replace('\\', '/', $class) . '.php';
require $file;
}
// Now use the fully qualified class name or import it
$class = new Person\Barnes\David\Class1();
// OR
use Person\Barnes\David\Class1;
$class = new Class1();
However, this manual path construction is error-prone. The modern standard, introduced in PHP 5.3 and refined in later versions, is to use a PSR-0 or PSR-4 compliant autoloader instead of the __autoload function. These standards define a strict mapping between namespace backslashes and directory separators, allowing frameworks to load classes automatically without you writing custom logic.
For example, using Composer (which implements PSR-4) would generate an autoloader that knows exactly where to find Person/Barnes/David/Class1.php whenever you instantiate the class, regardless of which folder your script is in. If you are maintaining legacy code without Composer, consider switching to a simple custom autoloader or utilizing spl_autoload_register with a function that handles the namespace resolution logic explicitly.
For modern Laravel applications, you should not manually write autoloaders. The framework includes a robust autoloading mechanism that leverages the PSR-4 standard. You can learn more about how Laravel handles class loading and namespaces in their official documentation at https://laravelcompany.com.
