Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
0
Language: PHP
Posted by: Сергей Шевченко
Added: Aug 19, 2018 12:41 PM
Modified: Aug 19, 2018 12:43 PM
Views: 4
Tags: autoload php spl
  1. spl_autoload_register(function ($class) {
  2.  
  3.     // namespace prefix
  4.     $prefix = 'prefix';
  5.     $base_dir = ROOT_DIR . '/inc/';
  6.  
  7.     $len = strlen($prefix);
  8.     if (strncmp($prefix, $class, $len) !== 0) {
  9.         return;
  10.     }
  11.     // get the relative class name
  12.     $relative_class = substr($class, $len);
  13.  
  14.     $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
  15.  
  16.     // if the file exists, require it
  17.     if (file_exists($file)) {
  18.         require $file;
  19.     }
  20. });