Skip to content

Commit e466523

Browse files
committed
Added autoloader
1 parent cc40499 commit e466523

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

autoload.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* I fully recommend to use this library with composer.
4+
* Use this autoloader only if you don't want to install composer.
5+
*/
6+
7+
spl_autoload_register(function ($class) {
8+
// project-specific namespace prefix
9+
$prefix = 'Screen\\';
10+
11+
// base directory for the namespace prefix
12+
$base_dir = __DIR__ . '/src/';
13+
14+
// does the class use the namespace prefix?
15+
$len = strlen($prefix);
16+
if (strncmp($prefix, $class, $len) !== 0) {
17+
// no, move to the next registered autoloader
18+
return;
19+
}
20+
21+
// get the relative class name
22+
$relative_class = substr($class, $len);
23+
24+
// replace the namespace prefix with the base directory, replace namespace
25+
// separators with directory separators in the relative class name, append
26+
// with .php
27+
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
28+
29+
// if the file exists, require it
30+
if (file_exists($file)) {
31+
require $file;
32+
}
33+
});

demo/shot.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
// Use the first autoload instead if you don't want to install composer
4+
//require_once '../autoload.php';
35
require_once '../vendor/autoload.php';
46

57
if (!isset($_GET['url'])) {

0 commit comments

Comments
 (0)