接着介绍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的封装调用。