Typecho源码分析(4)
上回说到,在require 'Typecho/Common.php'
文件后,立刻做了Typecho::init()
这个操作。
这个从名字上来说,肯定就是启动Typecho了,但具体怎么启动呢?其实就是这样的代码:
class Typecho_Common {
/**
* 自动载入类
*
* @param $className
*/
public static function __autoLoad($className)
{
@include_once str_replace(array('\\', '_'), '/', $className) . '.php';
}
/**
* 程序初始化方法
*
* @access public
* @return void
*/
public static function init()
{
/** 设置自动载入函数 */
spl_autoload_register(array('Typecho_Common', '__autoLoad'));
// 删去了一些兼容性代码
/** 设置异常截获函数 */
set_exception_handler(array('Typecho_Common', 'exceptionHandle'));
}
}
这代码啥意思呢?
首先,init
和__autoload
都是静态函数,不需要实例化这个Typecho_Common
class即可启动这个class里的静态方法。
第二,spl_autoload_register
,这个函数,第一个参数是的类型是callable
,所以这个array('Typecho_Common', '__autoLoad')
可以解析出来,具体请看PHP文档里对callable
的解释和示例,非常多的示例。
第三,还有一个问题,这里的__autoload
函数,必须要明确引入,否则不起作用,文档这么写了:
If your code has an existing __autoload() function then this function must be explicitly registered on the __autoload queue. This is because spl_autoload_register() will effectively replace the engine cache for the __autoload() function by either spl_autoload() or spl_autoload_call().
这个函数干了啥呢?把className中的_
或\
替换为/
,最后加上.php
这个extension,这样通过$className
来找到对应的文件,然后include
进来。
总之,执行init
之后,Tyecho初始化就完成了。
接下来,会发生什么事情呢?且听下回分解。