-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoLoad.php
48 lines (41 loc) · 1.49 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
<?php
// Define a class for autoloading other classes
class AutoLoad
{
// Singleton instance variable
private static $_instance = null;
// Private constructor to enforce singleton pattern and register the autoload function
private function __construct()
{
spl_autoload_register([$this, 'load']);
}
// Singleton instance creation method
public static function _instance()
{
// Create a new instance if it doesn't exist
if (!self::$_instance)
self::$_instance = new AutoLoad();
// Return the instance
return self::$_instance;
}
// Autoload function to include class files when needed
public function load($class)
{
// Define the paths for the class file and base class file
$classFile = trailingslashit(OOP_PLUGIN_DIR . 'class') . $class . '.php';
$baseClassFile = trailingslashit(OOP_PLUGIN_DIR . 'baseClass') . $class . '.php';
// Check if either class file or base class file is readable
if (is_readable($classFile) || is_readable($baseClassFile)) {
// Include the class file if it exists
if (file_exists($classFile)) {
include_once $classFile;
}
// Include the base class file if the class file doesn't exist
elseif (file_exists($baseClassFile)) {
include_once $baseClassFile;
}
}
}
}
// Create a single instance of the AutoLoad class
AutoLoad::_instance();