فهرست منبع

Add dependency injection to load classes

ohartl 9 سال پیش
والد
کامیت
3b8466ab7f
2فایلهای تغییر یافته به همراه10 افزوده شده و 6 حذف شده
  1. 1 1
      README.md
  2. 9 5
      include/php/default.inc.php

+ 1 - 1
README.md

@@ -116,7 +116,7 @@ Without subdirectory in URL (e.g. `http://webmum.mydomain.tld/`):
 </VirtualHost>
 ```
 
-Access to the codebase is denied with a `.htaccess` file under ^/include/php^.
+Access to the codebase is denied with a `.htaccess` file, that can be found in `/include/php`.
 
 
 

+ 9 - 5
include/php/default.inc.php

@@ -26,11 +26,15 @@ if($db->connect_errno > 0){
 	die('Unable to connect to database [' . $db->connect_error . ']');
 }
 
-/* Import models */
-require_once 'include/php/models/User.php';
-
-/* Import classes */
-require_once 'include/php/classes/Auth.php';
+// register automatic loading for dependency injection
+spl_autoload_register(function($class){
+	if(file_exists('include/php/models/'.$class.'.php')){
+		include 'include/php/models/'.$class.'.php';
+	}
+	elseif(file_exists('include/php/classes/'.$class.'.php')){
+		include 'include/php/classes/'.$class.'.php';
+	}
+});
 
 /* Initialize Authentication (Login User if in session) */
 Auth::init();