本文章来给各位介绍PHP MVC框架之加载器学习笔记,有需要了解的朋友可进入参考.上节说了MVC的路由原理,这节我们来说说MVC框架的loader,也称为加载器.虽然不同的框架,加载器的用法不同,但是其原理都是相通的…都是通过单例模式加载文件,而且把已加载的文件cache起来,避免重复加载…因为在开发业务的过程中,常常需要加载不同的模块,不同的类库等,MVC的loader可以帮助我们实现单例模式,更加符合开发需求.下面我们开始动手,利用其原理,做一个简单的loader.
首先,我们设计的加载器,要满足可以全局使用,无论在controller里还是在model、library里都可以使用.
其次,加载器要有缓存能力,即相同的文件只加载一次,相同的类只实例化一次.
最后,要能加载多种文件,model、library、helper.
先解决第一步,怎么全局使用,最简单的办法就是定义一个全局的function,这样,在后面的不同类库,不同function里都可以调用这个全局function,进行加载文件.
第二步和第三步,要一起解决,定义一个单例模式的loader类,因为是单例模式,可以保证全局只有一份内容,这样的话,就可以利用数组把这些已加载的内容和对象cache起来,这样就可以避免了重复加载的问题…
实例代码如下:
- if ( ! function_exists(‘load’))
- {
- function load($name, $type=’model’, $data=array())
- {
- static $loader = NULL;
- if (is_null($loader))
- {
- include (ROOT.’/system/core/loader.php’);
- $loader = Loader::get_instance();
- }
- return $loader->$type($name, $data);
- }
- }
上面这个,就是我写的一个简单的全局函数 load,这个函数的作用就是为了可以全局调用,然后通过这个load函数,去调用loader类,由loader类负责加载文件,实例化,和缓存对象…下面我把loader的代码放出来给大家看看.
实例代码如下:
- <?php
- class Loader
- {
- private $_loaded_models = array();
- private $_loaded_librarys = array();
- private static $OBJ;
- # 单例模式,不允许new对象
- private function __construct()
- {
- }
- public static function get_instance()
- {
- if (is_null(self::$OBJ))
- {
- $class = __CLASS__;
- self::$OBJ = new $class;
- }
- return self::$OBJ;
- }
- public function model($model, $data=array())
- {
- $model = strtolower($model);
- # 如果已经加载,则返回对象,避免重复加载
- if (isset($this->_loaded_models[$model]))
- {
- return $this->_loaded_models[$model];
- }
- # 否则加载文件
- include ROOT.“/application/models/{$model}.php”;
- $class = ucfirst($model);
- # 实例化对象
- if (emptyempty($data))
- {
- $instance = new $class;
- }
- else
- {
- $instance = new $class($data);
- }
- # 把对象cache起来
- $this->_loaded_models[$model] = $instance;
- return $instance;
- }
- public function library($library, $data=array())
- {
- $library = strtolower($library);
- # 如果已经加载,则返回对象,避免重复加载
- if (isset($this->_loaded_librarys[$library]))
- {
- return $this->_loaded_librarys[$library];
- }
- # 否则加载文件
- include ROOT.“/application/librarys/{$library}.php”;
- $class = ucfirst($library);
- # 实例化对象
- if (emptyempty($data))
- {
- $instance = new $class;
- }
- else
- {
- $instance = new $class($data);
- }
- # 把对象cache起来
- $this->_loaded_librarys[$library] = $instance;
- return $instance;
- }
- }
- ?>
大家看到了没有,这个loader采用的是单例模式,虽然前面的function的设计已经可以保证不会重复实例化loader这个对象,但是为了真正可以保证全局只有一份loader对象,设计成单例模式是最保险的…
具体的测试例子请看代码…我把代码打包上传上来,有需要的就下载回去看看吧…下载后,解压,把lesson2文件夹放到你的www目录下,然后访问yourdomain/MVC/lesson2/index.php/index/test 试试看吧…
波比源码 » PHP MVC框架之加载器学习笔记
buy levofloxacin 500mg buy levaquin 250mg pill
purchase avodart generic celecoxib brand purchase zofran online
aldactone price buy diflucan 100mg pills buy diflucan 100mg pill
sildenafil cost fildena 100mg without prescription buy robaxin 500mg
accutane us zithromax 250mg tablet purchase azithromycin sale
order anastrozole sale order biaxin 250mg pills cost sildenafil
furosemide 100mg sale generic lasix 40mg plaquenil canada
chloroquine online buy chloroquine 250mg pills buy olumiant 2mg sale
glucophage cost cialis coupon cvs buy generic tadalafil
buy generic cialis 40mg order sildenafil 100mg online cheap viagra online order
sumatriptan cheap purchase imitrex online avodart for sale
order generic desyrel 50mg trazodone tablet order generic sildenafil 100mg
retin over the counter buy tretinoin generic avanafil 200mg oral
buy albuterol for sale buy albuterol 100 mcg sale order ciprofloxacin 1000mg without prescription
montelukast 10mg ca sildenafil mail order us buy sildenafil 50mg for sale
cheap cialis without prescription Canadian generic cialis cialis 5mg cheap
altace uk generic ramipril order etoricoxib
doxycycline 200mg tablet buy clindamycin for sale cleocin 300mg cost
benicar price buy depakote 500mg buy divalproex 250mg without prescription
order digoxin 250mg pills order molnupiravir 200mg without prescription molnunat 200mg us
order doxycycline 200mg sale methylprednisolone price methylprednisolone 8 mg over counter
cost tadacip 10mg buy tadalafil 20mg generic cheap trimox 250mg
nexium us buy furosemide 100mg without prescription lasix pills
cialis overnight shipping usa Buy cialis in usa viagra brand
female cialis tadalafil viagra us buying ed pills online
order modafinil 100mg generic promethazine cost phenergan tablet
order deltasone 20mg online cheap deltasone us order amoxil sale
purchase absorica pills isotretinoin uk order ampicillin 250mg pills
fildena usa brand lyrica 75mg propecia 1mg usa
albuterol inhaler amoxiclav uk purchase amoxiclav without prescription
prednisolone 20mg pills buy prednisolone 40mg without prescription order furosemide pills
order dutasteride for sale order keflex 125mg online order xenical 120mg pill
order simvastatin 10mg online cheap sildenafil pills 200mg order sildalis generic
buy uroxatral 10mg generic buy diltiazem pill diltiazem over the counter
order zetia online ezetimibe buy online buy methotrexate