您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一步步编写PHP的Framework(七)

2024/2/27 16:39:40发布9次查看
之前我们在入口文件就直接调用了route::run(),这样做有不有什么问题呢?
答案是有的!
有时候在进行路由之前和之后需要进行一些额外的处理,如果按照之前在入口文件直接调用route::run()的话,那么这些处理过程只能写在入口文件中,但是入口文件本不应该做这样的事情,那么我们该怎么样来解决这个问题呢?
我们引入一个前端控制器的概念,它相当于一个总控,所有外部的请求都在它的控制范围内,那么这些额外的处理是不是就可以放在这个控制器中了呢!!
好,我们现在先修改一下入口文件:
01 run();
这段代码实际上修改的内容不多,也就修改了:
1 include framework_path . '/frontcontroller.php';
2 $frontcontroller = frontcontroller::getinstance();
3 $frontcontroller->run();
这段代码首先include了前端控制器这个文件,然后实例化了前端控制器,然后调用前端控制器这个类的run方法。
为什么不直接使用$frontcontroller = new frontcontroller()?
如果你把握后面的代码下载后运行会发现,这样会报错,为什么呢?
这儿我需要介绍另外一个概念:单例模式。
单例模式就是在整个程序运行过程中只有一个实例,比如数据库的连接,即使有很多类,但是可能这些类都公用数据库的连接,那么数据库的连接就是单例的。
为什么要使用单例呢?
大家可以想一下,前端控制器控制整个程序的运行,那么它的确是不应该有多份的,是不是?
 为了保证是单例的,所以我们一般使用一个静态方法(getinstance)来实例化它,为了防止用户直接new和克隆一个对象,我们需要将__construct和__clone设置为私有。
01 02 class test {
03     private static $_instance = null;
04     private function __construct() {}
05     private function __clone() {}
06     public static function getinstance() {
07         if(null === self::$_instance) {
08             self::$_instance = new test();
09             echo '1';
10         }
11         return self::$_instance;
12     }
13 }
14
15 $test = test::getinstance();
16 $test2 = test::getinstance();
如果执行这段代码,会发现只输出了一个1,说明实例化只执行了一次。
说了这么多,大家应该都应该懂了吧,那就直接贴出前端控制器的代码吧:
01 02 class frontcontroller {
03     private static $_instance = null;
04     private function __construct() {}
05     private function __clone() {}
06     public static function getinstance() {
07         if(!(self::$_instance instanceof self)) {
08             self::$_instance = new frontcontroller();
09         }
10         return self::$_instance;
11     }
12     public function run() {
13         route::run();
14     }
15 }
当然,前端控制器中我是使用了instanceof来判定的,其实也可以直接用null === self::$_instance来判定。
这段代码很简单,实际上就把route::run()转移到frontcontroller了,其他的都没有什么变化。
大家可能注意到了,入口文件的内容还是太多了,为什么不能把入口文件的东西迁移到frontcontroller呢,那么那些可以迁移到frontcontroller呢,首先是定义的常量,其实只要用户定义app_path即可,其他的常量都可以由框架默认,其次是配置文件的写入,这种内容框架完全可以自己搞定,所以,入口文件的代码变成了这样:
1 run();
我在此处留下了framework_path,是因为我要引用框架的文件,其实也不定义frmaework_path,而改用include app_path . '/library/test/frontcontroller.php'。
同样,frontcontroller的代码也发生了变化:
01 02 defined('app_path')  exit('未定义app_path');
03 defined('framework_path') define('framework_path',app_path . '/library/test');
04 defined('modules_path') define('modules_path',app_path . '/userapps/modules');
05 defined('configs_path') define('configs_path',app_path . '/userapps/configs');
06 include framework_path . '/function.php';
07 class frontcontroller {
08     private static $_instance = null;
09     private function __construct() {
10         c(config::factory(config::php)); //写入配置信息
11     }
12     private function __clone() {}
13     public static function getinstance() {
14         if(!(self::$_instance instanceof self)) {
15             self::$_instance = new frontcontroller();
16         }
17         return self::$_instance;
18     }
19     public function run() {
20         route::run();
21     }
22 }
首先,这个文件判定是否定义了app_path,由于这个常量非常重要,其他路径都依靠它,所以用户如果不指定,程序就需要停止。其他的常量如果没有定义,那么就定义它,这个很简单。
 配置文件写到了构造函数中了,因为不管getinstance调用多少次,构造函数只会被调用1次,而配置文件刚好也只需要写入一次。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product