Autoloader.php 987 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Workerman;
  3. if(!defined('WORKERMAN_ROOT_DIR'))
  4. {
  5. define('WORKERMAN_ROOT_DIR', realpath(__DIR__ . '/../'));
  6. }
  7. require_once WORKERMAN_ROOT_DIR.'/Workerman/Lib/Constants.php';
  8. class Autoloader
  9. {
  10. protected static $_appInitPath = '';
  11. public static function setRootPath($root_path)
  12. {
  13. self::$_appInitPath = $root_path;
  14. }
  15. public static function loadByNamespace($name)
  16. {
  17. $class_path = str_replace('\\', DIRECTORY_SEPARATOR ,$name);
  18. $class_file = self::$_appInitPath . '/' . $class_path.'.php';
  19. if(!is_file($class_file))
  20. {
  21. $class_file = WORKERMAN_ROOT_DIR . DIRECTORY_SEPARATOR . "$class_path.php";
  22. }
  23. if(is_file($class_file))
  24. {
  25. require_once($class_file);
  26. if(class_exists($name, false))
  27. {
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. }
  34. spl_autoload_register('\Workerman\Autoloader::loadByNamespace');