I'm trying to learn PHP better my working on my custom framework (learning purposes) and I was wondering how can I implement namespaces with aliases (they're new to me). My "framework" structure look like:
app/
controllers/
core/
functions/
languages/
models/
vendor/
views/
.htaccess
composer.json
composer.lock
config.php
database.php
filters.php
index.php
init.php
routes.php
style/
upload/
.htaccess
index.php
robots.txt
In app/core folder I have files:
App.php
Controller.php
Filter.php
Functions.php
Language.php
Route.php
index.php
And my init.php file looks like:
<?php
require "vendor/autoload.php";
require "database.php";
require "core/App.php";
require "core/Filter.php";
require "core/Route.php";
require "functions/Common.php";
require "functions/Validator.php";
require "functions/Custom.php";
require "core/Language.php";
require "core/Functions.php";
require "core/Controller.php";
Files from controllers/ folder extend Controller.php, and core/Functions.php extends core/Custom.php which extends core/Common.php How can I implement namespacing and alisaing so I can make my framework easier for maintaining and not needing to load every class with full namespace? Thanks in advance
Edit: Ok I've created a file controller.php in app/controllers/ with this code
<?php
use App\Core\Controller as BaseController;
class Controller extends BaseController {
}
And in my App.php I added the following function:
private function applyAliases() {
foreach(self::$config['aliases'] as $file => $alias) {
class_alias($file, $alias);
}
}
So in my config.php file i added:
/* Aliases */
"aliases" => [
"App\Core\Language" => "Language",
"App\Core\Filter" => "Filter",
"App\Core\Route" => "Route"
]
Also, in Functions.php when loading functions i added another class_alias:
public function loadFunction(string $file) {
if(!file_exists(App::$config['paths']['functions'].$file.".php")) {
throw new Exception("File ".App::$config['paths']['functions'].$file.".php doesn't exists");
}
require App::$config['paths']['functions'].$file.".php";
class_alias("App\Functions\\".$file, $file);
return new $file();
}
Would this be the proper way?