Skip to content

Commit 0a7e50c

Browse files
committed
Enhancement: Use PSR-4 autoloader
1 parent 38d4e07 commit 0a7e50c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

autoload.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/php-fig/fig-standards/blob/a1a0674a742c9d07c5dd450209fb33b115ee7b40/accepted/PSR-4-autoloader-examples.md#closure-example
5+
*/
6+
spl_autoload_register(static function (string $class): void {
7+
$prefix = 'phpweb\\';
8+
9+
$directory = __DIR__ . '/src/';
10+
11+
$length = strlen($prefix);
12+
13+
if (strncmp($prefix, $class, $length) !== 0) {
14+
return;
15+
}
16+
17+
$relativeClass = substr(
18+
$class,
19+
$length
20+
);
21+
22+
$file = $directory . str_replace('\\', '/', $relativeClass) . '.php';
23+
24+
if (!file_exists($file)) {
25+
return;
26+
}
27+
28+
require $file;
29+
});

include/shared-manual.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $PGI = array(); $SIDEBAR_DATA = '';
2323
// User note display functions
2424
// =============================================================================
2525

26-
require_once __DIR__ . '/../src/UserNotes/Sorter.php';
26+
require_once __DIR__ . '/../autoload.php';
2727
use phpweb\UserNotes\Sorter;
2828

2929
// Print out all user notes for this manual page

0 commit comments

Comments
 (0)