<?php
define('MPATH','mpath');
define('CPATH','cpath');
define('VPATH','vpath');
define('LPATH','lpath');
class Loader{
protected $_controllerDirectoryPath=array();
protected $_modelDirectoryPath=array();
protected $_libraryDirectoryPath=array();
public function __construct(){
$this->modelDirectoryPath=MPATH;
$this->viewDirectoryPath=VPATH;
$this->controllerDirectoryPath=CPATH;
$this->libraryDirectoryPath=LPATH;
spl_autoload_register(array($this,'load_controller'));
spl_autoload_register(array($this,'load_models'));
spl_autoload_register(array($this,'load_library'));
spl_autoload_register(array($this,'load_librarys'));
}
function load_library($library,$param=null){
if(is_string($library)){
return $this->initialize_class($library);
}
if(is_array($library)){
foreach($library as $key){
return $this->initialize_class($library);
}
}
}
public function initialize_class($library){
try{
if(is_array($library)){
foreach($library as $class){
$arrayObject=new $class;
}
return $this;
}
if(is_string($library)){
$stringObject=new $library;
}else{
throw new ISException('Class name must be string.');
}
if(null==$library){
throw new ISException('you must enter the name of the class');
}
}catch(Exception $e){
echo $e;
}
}
function load_controller($controller){
if($controller){
set_include_path($this->controllerDirectoryPath);
spl_autoload_extensions('.php');
spl_autoload($controller);
}
}
function load_models($model){
if($model){
set_include_path($this->modelDirectoryPath);
spl_autoload_extensions('.php');
spl_autoload($model);
}
}
function load_librarys($library){
if($library){
set_include_path($this->libraryDirectoryPath);
spl_autoload_extensions('.php');
spl_autoload($library);
}
}
}
$loader=new Loader();
$loader->load_controller('extender');
$base=new Extender();
$b=serialize($base);
echo $b;
?>
spl标准库自动加载文件和类
最新推荐文章于 2024-08-14 22:26:05 发布
本文介绍了一个简单的PHP类加载器实现方法,通过定义路径常量并利用spl_autoload_register进行类文件的自动加载。该加载器支持控制器、模型、库等不同类型的类加载。
1228

被折叠的 条评论
为什么被折叠?



