Answer the question
In order to leave comments, you need to log in
What do you think about the autoloader?
<?php
class Autoload
{
private array $prefixes = [
'prefix' => 'path/to/folder'
];
public function __construct()
{
spl_autoload_register(function ($class){
$this->load($this->getPath($class));
});
}
private function getPath(string $class)
{
return str_replace('\\', '/', $this->has($class) . '.php');
}
private function has(string $class)
{
$path = explode('\\', $class);
if (isset($this->prefixes[$path[0]])){
$class = str_replace($path[0], $this->prefixes[$path[0]], $class);
}
return $class;
}
private function load(string $path)
{
if (file_exists($path)) {
require_once $path;
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question