H
H
Human2020-06-11 02:26:44
PHP
Human, 2020-06-11 02:26:44

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;
        }
    }
}


Are there any shortcomings and mistakes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artemy Karkusha, 2020-06-11
@artemiy_karkusha

Use the composer autoloader.
More details:
Habr-Link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question