I am creating my own MVC framework for a simple website and I want to use namespaces but I don't know how to autoload them. Here is what I tried so far:
I have a init.php file that has autoloading function:
spl_autoload_register(function($class){
$file = __DIR__ . '/../' . str_replace('\\', '/', $class) .'.php';
echo $file;
if (file_exists($file)) {
require_once $file;
echo 'yes';
}
use App\Core\App as App;
$app = new App;
This code returns "yes" so it locates the App file.. But it also returns a fatal error: D:\xampp\htdocs\MVC\App/../App/Core/App.phpyes
Fatal error: Class 'App\Core\App' not found in D:\xampp\htdocs\MVC\App\init.php on line 28
Line 28 is $app = new App;
Here is how I am namespacing my App file:
<?php
namespace App\Core\App;
class App
{
.....
The file structure is the following:
| App
|--| Core
|--|--| App.php
...
| init.php
Any ideas why?
D:\xampp\htdocs\MVC\App\App\Core\App.php?D:/xampp/htdocs/MVC/App/Core/App.phpD:\xampp\htdocs\MVC\App/../App/Core/App.phpnamespace App\Core;namespace App\Core\App;and your class isAppit would be$app = new App\Core\App\App(); can you see how that's confusing lol