Zoals jullie wel weten heeft PHP5 autloading, waarbij klassen on-the-fly geladen worden als ze nodig zijn. Voor een persoonlijk project heb ik een classloader-klasse gemaakt die dit versimpelt. Je include de klasse, geeft aan waar de rest van je klassen zijn te vinden en klaar ben je.
Om de wereld een stukje beter te maken (kleeein stukje) wil ik hem graag met jullie delen. Oh ja, feedback is natuurlijk ook welkom
classloader.php:
Voorbeeld van gebruik
/my/class/dir/someotherdirwhichdoesnotmatter/Foo.php
index.php
Veel plezier ermee
http://www.bartvanheukelom.nl/fastmvc/index.php/Classloader
Om de wereld een stukje beter te maken (kleeein stukje) wil ik hem graag met jullie delen. Oh ja, feedback is natuurlijk ook welkom
classloader.php:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
| <?php /** * Include at the beginning of your app and call * ClassLoader::addPath($pathToClassesDirectory); */ /** * A static class containing<br/> * - a list of classes<br/> * - a function to add the contents of a directory to the list<br/> * - a function to include a specific classfile<br/> * Used with __autoload() or spl_autoload_register(), this provides * automatic class loading, without worries. * @author Bart van Heukelom */ class ClassLoader { private static $classList = array(); /** * Add the contents of a directory to the class list. This directory should * contain only class files following the naming format "ClassName.php". It does * not matter if they are directly in the directory, or in a subdirectory. * You can also choose to use directories as "namespaces". For example, * if you call addPath('/path', '_'), the file '/path/Foo/Bar.php' must * contain the class 'Foo_Bar'. This mode is mainly for compatibility with some * libraries. * @param string $directory The path to a directory of classes to add. * @param string $separator The namespace separator to optionally use */ public static function addPath($directory, $separator = null, $base = null) { if ($directory{strlen($directory) - 1} != DIRECTORY_SEPARATOR) { $directory .= DIRECTORY_SEPARATOR; // add trailing slash } if(!is_dir($directory)) { return; } $handle = opendir($directory); while ($file = readdir($handle)) { $fileLen = strlen($file); $completeFile = $directory . $file; if ($file{0} == '.') { continue; } if (is_dir($directory . $file)) { if (!is_null($separator)) { if (is_null($base)) { $newbase = $file; } else { $newbase = $base . $separator . $file; } } else { $newbase = null; } self::addPath($completeFile, $separator, $newbase); } else if (is_file($completeFile) && substr($file, $fileLen - 4) == '.php') { $className = substr($file, 0, $fileLen - 4); if (!is_null($separator)) { $className = $base . $separator . $className; } if (isset(self::$classList[$className])) { throw new Exception('Class file found twice: ' . $className); } self::$classList[$className] = realpath($completeFile); } } closedir($handle); } /** * Load a class, if it exists. * @param string $className The class to load. */ public static function load($className) { if(isset(self::$classList[$className])) { require_once self::$classList[$className]; } } } // neccesary, the class loader won't be used by PHP if this is not done spl_autoload_register(array('ClassLoader', 'load')); ?> |
Voorbeeld van gebruik
/my/class/dir/someotherdirwhichdoesnotmatter/Foo.php
PHP:
1
2
3
4
5
6
7
8
9
| <?php class Foo { public static function bar() { echo 'Hello world'; } } ?> |
index.php
PHP:
1
2
3
4
5
6
7
8
9
| <?php require 'classloader.php'; ClassLoader::addPath('/my/class/dir'); // now the file /my/class/dir/someotherdirwhichdoesnotmatter/Foo.php will automatically be loaded Foo::bar(); ?> |
Veel plezier ermee
http://www.bartvanheukelom.nl/fastmvc/index.php/Classloader