forked from nicke/php-autoloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoloader.php
132 lines (125 loc) · 4.09 KB
/
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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Registers an Autoloader
*
* The Autoloader works out of the box as simple as possible. You have
* nothing more to do than include the file autoloader.php. Don't bother the
* time it consumes when it's called the first time. Let it build its index.
* The second time it will run as fast as light.
*
* The simplest and probably most common use case shows this example:
*
* ./index.php
* <code>
* <?php
* require __DIR__ . "/autoloader/autoloader.php";
* $myObject = new MyClass();
* </code>
*
* ./classes/MyClass.php
* <code>
* <?php
* class MyClass extends MyParentClass
* {
*
* }
* </code>
*
* ./classes/MyParentClass.php
* <code>
* <?php
* class MyParentClass
* {
*
* }
* </code>
*
* As you can see it's only necessary to require autoloader.php once.
* If this is done in the document root of your classes (index.php in
* this case) the Autoloader is already configured. After requiring
* this file you don't have to worry where your classes reside.
*
* Another use case would have the class path outside of your document root.
* As the autoloader has no knowledge of this arbitrary path you have to
* tell him where your class path is:
*
* <code>
* <?php
*
* use malkusch\autoloader\Autoloader;
*
* require __DIR__ . "/autoloader/autoloader.php";
*
* // As the guessed class path is wrong you should remove this Autoloader.
* Autoloader::getRegisteredAutoloader()->remove();
*
* // register your arbitrary class path
* $autoloader = new Autoloader($classpath);
* $autoloader->register();
*
* // You might also have more class paths
* $autoloader2 = new Autoloader($classpath2);
* $autoloader2->register();
* </code>
*
* If you have the possibility to enable PHP's tokenizer you should do
* this. Otherwise the Autoloader has to use a parser based on PCRE
* which is not as reliable as PHP's tokenizer.
*
* The Autoloader assumes that a class name is unique. If you have classes with
* equal names the behaviour is undefined.
*
* PHP version 5
*
* LICENSE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.
* If not, see <http://php-autoloader.malkusch.de/en/license/>.
*
* @category PHP
* @package Autoloader
* @author Markus Malkusch <[email protected]>
* @copyright 2009 - 2010 Markus Malkusch
* @license http://php-autoloader.malkusch.de/en/license/ GPL 3
* @version SVN: $Id$
* @link http://php-autoloader.malkusch.de/en/
* @see Autoloader
*/
namespace malkusch\autoloader;
/**
* Another library might use this Autoloader in another package as well. In that
* case a copy of the Autoloader package exists at a different location in the
* file system. If the Autoloader classes are already loaded by that copy an
* include_once would still include the files of this copy, which would lead
* to a fatal error. To avoid this, the classes are only loaded if the class
* Autoloader is not defined yet.
*/
if (! class_exists('malkusch\autoloader\Autoloader')) {
include_once __DIR__ . "/Autoloader/OldPHPAPI.php";
$__oldAPI = new OldPHPAPI();
$__oldAPI->checkAPI();
unset($__oldAPI);
include_once __DIR__ . "/Autoloader/Autoloader.php";
}
/**
* A new instance of Autoloader is created and registered into the spl_autoload()
* stack. The class path of that instance is the directory of the file which
* has included this file.
*
* @see Autoloader::__construct()
* @see Autoloader::getCallersPath()
* @see Autoloader::register()
*/
$__autoloader = new Autoloader();
$__autoloader->register();
unset($__autoloader);