Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. <?php
  2. /**
  3. * An example of a project-specific implementation.
  4. *
  5. * After registering this autoload function with SPL, the following line
  6. * would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
  7. * from /path/to/project/src/Baz/Qux.php:
  8. *
  9. * new \Foo\Bar\Baz\Qux;
  10. *
  11. * @param string $class The fully-qualified class name.
  12. * @return void
  13. */
  14. spl_autoload_register(function ($class) {
  15.  
  16. // project-specific namespace prefix
  17. $prefix = '';
  18.  
  19. // base directory for the namespace prefix
  20. $base_dir = __DIR__ . DIRECTORY_SEPARATOR;
  21.  
  22. // does the class use the namespace prefix?
  23. $len = strlen($prefix);
  24. if (strncmp($prefix, $class, $len) !== 0) {
  25. // no, move to the next registered autoloader
  26. return;
  27. }
  28.  
  29. // get the relative class name
  30. $relative_class = substr($class, $len);
  31.  
  32. // replace the namespace prefix with the base directory, replace namespace
  33. // separators with directory separators in the relative class name, append
  34. // with .php
  35. $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
  36. // if the file exists, require it
  37. if (file_exists($file)) {
  38. require $file;
  39. }
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement