-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathautoloader.php
37 lines (33 loc) · 972 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
31
32
33
34
35
36
37
<?php
namespace LunarPHP;
/**
*
* 自动载入函数
* User: ljy
* Date: 17-08-18
*/
class Autoloader
{
const NAMESPACE_PREFIX = 'LunarPHP\\';
public static function register()
{
spl_autoload_register(array(new self, 'autoload'));
}
/**
* 根据类名载入所在文件
*/
public static function autoload($className)
{
$namespacePrefixStrlen = strlen(self::NAMESPACE_PREFIX);
if ( strncmp(self::NAMESPACE_PREFIX, $className, $namespacePrefixStrlen) === 0 ){
$className = strtolower($className);
$filePath = str_replace('\\', DIRECTORY_SEPARATOR, substr($className, $namespacePrefixStrlen));
$filePath = realpath(__DIR__ . ( empty($filePath) ? '' : DIRECTORY_SEPARATOR) . $filePath . '.lrp.php' );
if ( file_exists($filePath) ) {
require_once $filePath;
} else {
echo $filePath;
}
}
}
}