1 | <?php |
||
2 | /** |
||
3 | * An example of a project-specific implementation. |
||
4 | * |
||
5 | * After registering this autoload function with SPL, the following line |
||
6 | * would cause the function to attempt to load the \Foo\Bar\Baz\Qux class |
||
7 | * from /path/to/project/src/Baz/Qux.php: |
||
8 | * |
||
9 | * new \Foo\Bar\Baz\Qux; |
||
10 | * |
||
11 | * @param string $class The fully-qualified class name. |
||
12 | * @return void |
||
13 | */ |
||
14 | spl_autoload_register( |
||
15 | function ($class) { |
||
16 | |||
17 | // project-specific namespace prefix |
||
18 | //$prefix = 'Foo\\Bar\\'; |
||
19 | 1 | $prefix = 'Guer\\'; |
|
20 | |||
21 | // base directory for the namespace prefix |
||
22 | 1 | $base_dir = __DIR__ . '/src/'; |
|
23 | |||
24 | // does the class use the namespace prefix? |
||
25 | 1 | $len = strlen($prefix); |
|
26 | 1 | if (strncmp($prefix, $class, $len) !== 0) { |
|
27 | // no, move to the next registered autoloader |
||
28 | return; |
||
29 | } |
||
30 | |||
31 | // get the relative class name |
||
32 | 1 | $relative_class = substr($class, $len); |
|
33 | |||
34 | // replace the namespace prefix with the base directory, replace namespace |
||
35 | // separators with directory separators in the relative class name, append |
||
36 | // with .php |
||
37 | 1 | $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; |
|
38 | |||
39 | // if the file exists, require it |
||
40 | 1 | if (file_exists($file)) { |
|
41 | 1 | include $file; |
|
42 | 1 | } |
|
43 | 1 | } |
|
44 | ); |
||
45 |