-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoload.php
54 lines (45 loc) · 1.37 KB
/
autoload.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
<?php
namespace src;
class Autoloader {
/**
* Autoloader constructor.
* This constructor calls spl_autoload_register method with autoload method
* inside Autoloader class.
*
* @access public
*/
public function __construct() {
spl_autoload_register( array( $this, 'autoload' ) );
echo " auto loader start";
}
public function autoload( $class_name ) {
$file_parts = explode ( '\\', $class_name);
// Do a reverse loop through $file_parts to build a path to the file.
// mapping through the namespace and change it to folder name.
$namespace = '';
$file_name = '';
$count = count( $file_parts ) - 1 ;
for ( $i = $count ;$i > (-1); $i -- ) {
$current = strtolower ( $file_parts[$i] );
$current = str_ireplace ( '_', '-', $current);
if ( $count === $i ){
$file_name = "{$current}.php";
}
else{
$namespace = '/' . $current . $namespace;
}
}
// Build a path to the file using mapping to the file location.
$file_path = dirname ( dirname ( __FILE__)) . $namespace;
// file name MUST be same as class name. YOU CAN CHANGE THE RULE FROM HERE.
$file_path = $file_path . '/' . $file_name;
// If the file exists in the specified path, then include it.
if ( file_exists ( $file_path)){
include_once $file_path;
} else{
echo ( "The file attempting to be loaded at $file_path does not exist." ) ;
return;
}
}
}
new Autoloader();