R
R
Ruslan Karetsky2020-04-29 20:21:00
PHP
Ruslan Karetsky, 2020-04-29 20:21:00

How to correctly implement autoloading in PHP without sacrificing the use of use?

I must say right away that I know about the possibility of using a composer. But I want to learn how to do it myself and then explore the ready-made tools.
My problem is that I wrote the following code:

function loader($class)
{
    $class = str_replace('\\', '/', $class);
    $file = SITE_PATH . DS . $class .".php";

    if (file_exists($file)) {
        include $file;
        return true;
    } else {
        return false;
    }
}

spl_autoload_register('loader');


Now only those classes that are written as follows participate in autoloading: I can't use use, which really annoys me. To prescribe in this way is considered bad manners.

$registry = new core\Registry();


spl_autoload_register('loaderControllers');
spl_autoload_register('loaderModels');
spl_autoload_register('loaderViews');


How to solve the problem? How to register once spl_autoload_register(); without sacrificing the ability to access classes using use and not regularly add their full namespace?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Northern Lights, 2020-04-29
@php666

Reframe your question.
It's unclear.
Autoload and namespaces are not connected in any way, what exactly is the gag?
What means

I can't use use, which really annoys me.

G
Gluck Virtualen, 2020-04-30
@gluck59

"I have a suitcase full of tools. I need to tighten a bolt, but I can't sacrifice using a screwdriver."

Decipher pliz who is your victim and why.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question