-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutoloader.php
30 lines (28 loc) · 840 Bytes
/
Autoloader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/**
* Autoloader class to be used throughout the examples
*
* @package ClassicComputerScienceProblemsInPhp
*/
class Autoloader {
/**
* Autoloader method for all classes in the examples
*
* @param string $className The class to load
* @throws UnexpectedValueException if a class does not exist
*/
public static function autoload($className) {
if (!class_exists($className)) {
$iterator = new RecursiveDirectoryIterator(__DIR__);
foreach (new RecursiveIteratorIterator($iterator) as $entry) {
if ($entry->getFilename() == $className.'.php') {
require_once($entry->getPathname());
return;
}
}
throw new UnexpectedValueException("Class $className not found.");
}
}
}
// Register the autoloader method
spl_autoload_register(['Autoloader', 'autoload']);