Код: Выделить всё
spl_autoload_register(function (string $class_name): void {
// Get mappings from json
$conductor = file_get_contents(__DIR__.'/autoload.json');
$conductor = str_replace('\\', '\\\\', $conductor);
$configuration = json_decode(stripslashes($conductor), true);
// Map the namespace to the corresponding folder
$namespace_mapping = $configuration['autoload']['psr-4'];
foreach ($namespace_mapping as $namespace => $directory) {
if (
strpos($class_name, $namespace = trim($namespace, '\\')) !== 0
|| (!$directory = realpath(__DIR__ . DIRECTORY_SEPARATOR . trim($directory, DIRECTORY_SEPARATOR)))
) {
continue; // Class name doesn't match or the directory doesn't exist
}
// Require the file
$class_file = $directory . str_replace([$namespace, '\\'], ['', DIRECTORY_SEPARATOR], $class_name) . '.php';
if (file_exists($class_file)) {
require_once $class_file;
}
}
});
< /code>
текущий json < /p>
{
"autoload": {
"psr-4": {
"Psr\\Log\\": "psr/log/src/",
"Psr\\Http\\Server\\": "psr/http-server-handler/src/",
"Psr\\Http\\Server\\": "psr/http-server-middleware/src/",
"Psr\\Http\\Message\\": "psr/http-message/src/",
"Psr\\Http\\Message\\": "psr/http-factory/src/",
"Psr\\Container\\": "psr/container/src/"
}
}
}
< /code>
Я думал, что нужно сделать что -то подобное в JSON: < /p>
"Psr\\Http\\Server\\": ["psr/http-server-handler/src/", "psr/http-server-middleware/src/"],
Подробнее здесь: https://stackoverflow.com/questions/795 ... -namespace