There is a folder with independent modules, approximately as in wordpress.

autoload is no longer used. How can it learn about the path to the class by its name? Write a module manager as a type

ModuleManager::inc('имя модуля', 'имя класса'); 

It doesn’t look very nice, scan all the folders of the modules to find the classes also have a so-so solution (except to cache, but it still seems that there is a better solution)

Closed due to the fact that it is necessary to reformulate the question so that you can give an objectively correct answer to the participants Dmitriy Simushev , aleksandr barakin , user207618, user194374, Alex 25 Dec '16 at 22:20 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    First you need to draw the structure of your project, where it lies. And then strictly follow these rules.

    One of the most important and correct rules here will be to use for namespace classes that coincide with real paths.

    For example, the class lies in class/modules/db/Connect.php , then we set the class like this:

     namespace modules\db; class Connect{} 

    And then you can use something like this:

     const BASE_CLASS_PATH = '/path/to/base/class/directory'; spl_autoload_register( function ($class) { $class = '/class/'.str_replace('\\', '/', $class).'.php'; if (is_file(__DIR__.$class)) { include_once $class; } elseif (is_file(BASE_CLASS_PATH.$class)) { include_once $class; } } ); 
    • one
      spl_autoload_register() позволяСт Π·Π°Π΄Π°Ρ‚ΡŒ нСсколько Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° Π°Π²Ρ‚ΠΎΠ·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ описаний классов ΠΈ интСрфСйсов. Она создаСт ΠΎΡ‡Π΅Ρ€Π΅Π΄ΡŒ ΠΈΠ· Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ Π°Π²Ρ‚ΠΎΠ·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ Π² порядкС ΠΈΡ… опрСдСлСния Π² скриптС, Ρ‚ΠΎΠ³Π΄Π° ΠΊΠ°ΠΊ встроСнная функция __autoload() ΠΌΠΎΠΆΠ΅Ρ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄Π½Ρƒ Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΡŽ. spl_autoload_register() позволяСт Π·Π°Π΄Π°Ρ‚ΡŒ нСсколько Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° Π°Π²Ρ‚ΠΎΠ·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ описаний классов ΠΈ интСрфСйсов. Она создаСт ΠΎΡ‡Π΅Ρ€Π΅Π΄ΡŒ ΠΈΠ· Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ Π°Π²Ρ‚ΠΎΠ·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ Π² порядкС ΠΈΡ… опрСдСлСния Π² скриптС, Ρ‚ΠΎΠ³Π΄Π° ΠΊΠ°ΠΊ встроСнная функция __autoload() ΠΌΠΎΠΆΠ΅Ρ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄Π½Ρƒ Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΡŽ. php.net/manual/ru/function.spl-autoload-register.php - Jean-Claude

    Use composer , it covers 100% of the needs

    You will be able to use both psr-0 and psr-4 Your task is to include autoload.php in the head file and paint the config in composer.json

    psr-0:

     "autoload": { "psr-0": { "Monolog\\": "src/", "Vendor\\Namespace\\": "src/", "Vendor_Namespace_": "src/" }, "classmap": [ "config", "core", "modules" ], "files": [ "src/MyLibrary/functions.php" ] } 

    psr-4

     { "autoload": { "psr-4": { "Monolog\\": "src/", "Vendor\\Namespace\\": "" } } }