zoukankan      html  css  js  c++  java
  • 自己动手写个小框架之三

         接着介绍Loader类,即kernel里的autoload.php。

     1 <?php
     2 final class Loader{
     3     protected $register;
     4     
     5     public function __construct($register){
     6         $this->register = $register;
     7     }
     8     
     9     public function __get($key){
    10         return $this->register->get($key);
    11     }
    12     
    13     public function __set($key, $value){
    14         $this->register->set($key, $value);
    15     }
    16     
    17     public function register($type,$library){
    18         $path = $_SERVER['DOCUMENT_ROOT'].'/dluf/';
    19         $file = $path.'library/'.$library;
    20         switch ($type){
    21             case 'library':
    22                 break;
    23             case 'controller':
    24                 $file = $path.'controller/'.$library.'Controller.php';
    25                 break;
    26             default :
    27                 break;
    28         }
    29         
    30         if(file_exists($file)){
    31             try{
    32                 include_once($file);
    33             }  catch (Exception $exc){
    34                 echo $exc;
    35             }
    36         }else{
    37             trigger_error("Error:file can not find $library.!");
    38             exit();
    39         }
    40     }
    41     
    42     
    43     public function config(){
    44         $file = __DIR__.'config/config.php';
    45         include_once($file);
    46     }
    47 }
    48 ?>
    public function register($type,$library)函数用于动态加载相应的类,type类型中“library”为以后扩展用,这里我们主要关心控制类Controller的加载。
    30         if(file_exists($file)){
    31             try{
    32                 include_once($file);
    33             }  catch (Exception $exc){
    34                 echo $exc;
    35             }
    36         }else{
    37             trigger_error("Error:file can not find $library.!");
    38             exit();
    39         }

    若文件存在,则对其加载include_once($file)。
    系列四中将介绍对smarty的封装调用。


  • 相关阅读:
    高精度
    eps取值
    QtCreator常用快捷键
    C++11 麻辣烫
    谈一些关于华为鸿蒙的看法
    Termux新手基础+进阶学习笔记(间断更新)
    分享互联网2021年最新Java面试题汇总整理-附详细答案解析
    2021年面试,整理全网初、中、高级常见Java面试题附答案
    JS中使用map
    springboot整合MongoDB4.0多数据源实现事务
  • 原文地址:https://www.cnblogs.com/dluf/p/3045424.html
Copyright © 2011-2022 走看看