1
0
mirror of https://github.com/php/web-php.git synced 2026-03-23 23:02:13 +01:00

Enhancement: Use PSR-4 autoloader (#604)

This commit is contained in:
Andreas Möller
2022-07-05 13:50:41 +02:00
committed by GitHub
parent 870dee1e16
commit a83ae2fa32
4 changed files with 31 additions and 3 deletions

28
autoload.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
/**
* @see https://github.com/php-fig/fig-standards/blob/a1a0674a742c9d07c5dd450209fb33b115ee7b40/accepted/PSR-4-autoloader-examples.md#closure-example
*/
spl_autoload_register(static function (string $class): void {
$prefix = 'phpweb\\';
$directory = __DIR__ . '/src/';
$length = strlen($prefix);
if (strncmp($prefix, $class, $length) !== 0) {
return;
}
$relativeClass = substr(
$class,
$length
);
$file = $directory . str_replace('\\', '/', $relativeClass) . '.php';
if (!file_exists($file)) {
return;
}
require $file;
});

View File

@@ -2,7 +2,7 @@
<?php
PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
require_once __DIR__ . '/../src/News/Entry.php';
require_once __DIR__ . '/../autoload.php';
use phpweb\News\Entry;

View File

@@ -2,7 +2,7 @@
<?php
PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
require_once __DIR__ . '/../src/News/Entry.php';
require_once __DIR__ . '/../autoload.php';
use phpweb\News\Entry;

View File

@@ -23,7 +23,7 @@ $PGI = array(); $SIDEBAR_DATA = '';
// User note display functions
// =============================================================================
require_once __DIR__ . '/../src/UserNotes/Sorter.php';
require_once __DIR__ . '/../autoload.php';
use phpweb\UserNotes\Sorter;
// Print out all user notes for this manual page