think\Loader::import PHP Method

import() public static method

导入所需的类库 同java的Import 本函数有缓存功能
public static import ( string $class, string $baseUrl = '', string $ext = EXT ) : boolean
$class string 类库命名空间字符串
$baseUrl string 起始路径
$ext string 导入的文件扩展名
return boolean
    public static function import($class, $baseUrl = '', $ext = EXT)
    {
        static $_file = [];
        $key = $class . $baseUrl;
        $class = str_replace(['.', '#'], [DS, '.'], $class);
        if (isset($_file[$key])) {
            return true;
        }
        if (empty($baseUrl)) {
            list($name, $class) = explode(DS, $class, 2);
            if (isset(self::$prefixDirsPsr4[$name . '\\'])) {
                // 注册的命名空间
                $baseUrl = self::$prefixDirsPsr4[$name . '\\'];
            } elseif ('@' == $name) {
                //加载当前模块应用类库
                $baseUrl = App::$modulePath;
            } elseif (is_dir(EXTEND_PATH . $name)) {
                $baseUrl = EXTEND_PATH . $name . DS;
            } else {
                // 加载其它模块的类库
                $baseUrl = APP_PATH . $name . DS;
            }
        } elseif (substr($baseUrl, -1) != DS) {
            $baseUrl .= DS;
        }
        // 如果类存在 则导入类库文件
        if (is_array($baseUrl)) {
            foreach ($baseUrl as $path) {
                $filename = $path . DS . $class . $ext;
                if (is_file($filename)) {
                    break;
                }
            }
        } else {
            $filename = $baseUrl . $class . $ext;
        }
        if (!empty($filename) && is_file($filename)) {
            // 开启调试模式Win环境严格区分大小写
            if (IS_WIN && pathinfo($filename, PATHINFO_FILENAME) != pathinfo(realpath($filename), PATHINFO_FILENAME)) {
                return false;
            }
            __include_file($filename);
            $_file[$key] = true;
            return true;
        }
        return false;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * 架构函数
  * @access public
  */
 public function __construct()
 {
     //控制器初始化
     if (method_exists($this, '_initialize')) {
         $this->_initialize();
     }
     //导入类库
     \think\Loader::import('vendor.Hprose.HproseHttpServer');
     //实例化HproseHttpServer
     $server = new \HproseHttpServer();
     if ($this->allowMethodList) {
         $methods = $this->allowMethodList;
     } else {
         $methods = get_class_methods($this);
         $methods = array_diff($methods, array('__construct', '__call', '_initialize'));
     }
     $server->addMethods($methods, $this);
     if (APP_DEBUG || $this->debug) {
         $server->setDebugEnabled(true);
     }
     // Hprose设置
     $server->setCrossDomainEnabled($this->crossDomain);
     $server->setP3PEnabled($this->P3P);
     $server->setGetEnabled($this->get);
     // 启动server
     $server->start();
 }
All Usage Examples Of think\Loader::import