thinkphp系列:类的自动加载是如何设计的
在使用框架开发时,可以发现框架有很多核心类,却很少看到显示的引入某个文件的代码,这是因为框架都采用了类的自动加载机制,即使用到类时,框架会自动找到该类所在文件的位置并引入该文件。
为了更容易看出代码思路,下面在说明时,只抽取了相关的主要代码。
在剖析thinkphp源码之前,先说说我做的一个项目实现的自动加载思路。
根据文件命名特点来确定文件所在的位置。
入口文件代码:
//入口文件index.php
require_once('base.php');
if(function_exists('spl_autoload_register')) {
spl_autoload_register(array('Base', 'autoload'));
} else {
function __autoload($class) {
return Base::autoload($class);
}
}
//base.php
final class Base{
public static function autoload($class){
$class = strtolower($class);
if (ucwords(substr($class,0,5)) == 'Cache' && $class != 'cache'){
if (!@include_once(BASE_CORE_CACHE_PATH.DS.substr($class,0,5).'.'.substr($class,5).'.php')){
exit("Class Error: {$class}.isn't exists!");
}
}elseif ($class == 'db'){
if (!@include_once(BASE_CORE_PATH.DS.'db'.DS.strtolower(DBDRIVER).'.php')){
exit("Class Error: {$class}.isn't exists!");
}
}else{
if (!@include_once(BASE_LIB_PATH.DS.$class.'.php')){
exit("Class Error: {$class}.isn't exists!");
}
}
}
}
如代码所示,所用的类带Cache时,就从BASE_CORE_CACHE_PATH这里寻找类文件。默认就从BASE_LIB_PATH这里寻找。
现在再让我们一起来看看thinkphp框架是怎么做的。
//start.php入口文件
namespace think;
// ThinkPHP 引导文件
// 加载基础文件
require 'base.php';
// 执行应用
App::run()->send();
//base.php 文件
// 载入Loader类
require 'loader.php';
// 注册自动加载
\think\Loader::register();
//loader.php文件
namespace think;
class Loader{
public static function register($autoload = '')
{
spl_autoload_register($autoload ?: 'think\\Loader::autoload', true, true);
}
// 自动加载
public static function autoload($class)
{
echo 'enter autoload<br>';
echo $class[0].'<br>';
var_dump($class);
}
}
通过如上流程走下来,可以最终知道框架的自动加载功能实现主要在Loader类的autoload方法处。
以上代码可以自建一个小项目运行,假如项目名为test,浏览器里访问
http://localhost/test/start.php
可以得到如下结果:
enter autoload
t
string(9) "think\App" Fatal error: Class 'think\App' not found in E:\xampp\htdocs\test\start.php on line 9
从结果可以判断出,当执行
App::run()->send();
此处代码时,框架已成功进入实现自动加载机制的方法里去了。
然后就仔细了解下thinkphp框架是怎么引入这个App类所在的文件。
将如上文件代码更加详细化,如下所示:
//start.php入口文件
namespace think;
define('DS', DIRECTORY_SEPARATOR);
define('EXT', '.php');
define('LIB_PATH', __DIR__ .DS.'library'.DS);
// ThinkPHP 引导文件
// 加载基础文件
require 'base.php';
// 执行应用
App::run()->send();
接着新建App类文件,如果成功进入run方法,则表示App类自动加载成功。
<?php
//library/think/App.php
namespace think;
class App
{
public static function run()
{
echo 'enter run<br>';
}
}
loader.php文件修改稍微多些,请看代码:
<?php
//loader.php文件
namespace think;
class Loader{
// PSR-4
private static $prefixLengthsPsr4 = [];
private static $prefixDirsPsr4 = [];
public static function register($autoload = '')
{
spl_autoload_register($autoload ?: 'think\\Loader::autoload', true, true);
// 注册命名空间定义
self::addNamespace([
'think' => LIB_PATH . 'think' . DS,
'behavior' => LIB_PATH . 'behavior' . DS,
'traits' => LIB_PATH . 'traits' . DS,
]);
var_dump(self::$prefixLengthsPsr4);
}
// 自动加载
public static function autoload($class)
{
echo 'enter autoload<br>';
// echo $class[0].'<br>';
// var_dump($class);
if ($file = self::findFile($class)) {
include($file);
return true;
}
}
/**
* 查找文件
* @param $class
* @return bool
*/
private static function findFile($class)
{
// 查找 PSR-4
$logicalPathPsr4 = strtr($class, '\\', DS) . EXT;
$first = $class[0];
if (isset(self::$prefixLengthsPsr4[$first])) {
foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach (self::$prefixDirsPsr4[$prefix] as $dir) {
if (is_file($file = $dir . DS . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
}
// 注册命名空间
public static function addNamespace($namespace, $path = '')
{
if (is_array($namespace)) {
foreach ($namespace as $prefix => $paths) {
self::addPsr4($prefix . '\\', rtrim($paths, DS), true);
}
} else {
self::addPsr4($namespace . '\\', rtrim($path, DS), true);
}
}
// 添加Psr4空间
private static function addPsr4($prefix, $paths, $prepend = false)
{
if (!$prefix) {
} elseif (!isset(self::$prefixDirsPsr4[$prefix])) {
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
self::$prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
self::$prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) {
} else {
}
}
}
base.php文件内容不变。
现在我们梳理一下代码执行顺序(从上往下依次执行):
---\think\Loader::register()
---spl_autoload_register('think\\Loader::autoload')
---\think\Loader::addNamespace()
---\think\Loader::addPsr4()
---\think\Loader::autoload()
---\think\Loader::findFile()
---App::run()
其中addNamespace里addPsr4方法将部分命名空间对应的实际目录存储进了static数组变量中。
打印$prefixLengthsPsr4和$prefixDirsPsr4这两个变量的内容,得到如下所示:
array(2) {
["t"]=>
array(2) {
["think\"]=>int(6)
["traits\"]=>int(7)
}
["b"]=>
array(1) {
["behavior\"]=>int(9)
}
}
array(3) {
["think\"]=>
array(1) {
[0]=>
string(34) "E:\xampp\htdocs\test\library\think"
}
["behavior\"]=>
array(1) {
[0]=>
string(37) "E:\xampp\htdocs\test\library\behavior"
}
["traits\"]=>
array(1) {
[0]=>
string(35) "E:\xampp\htdocs\test\library\traits"
}
}
然后到了autoload里findFile这步,分析如下:
//之前测试autoload方法,得知$class = 'think\App'; $class[0] = 't';
//将$class字符串里反斜杠替换成文件分隔符,
//再接上文件后缀,变成'think/App.php'
//注意:$class变量里仍然是命名空间分隔符
$logicalPathPsr4 = strtr($class, '\\', DS) . EXT;
$first = $class[0];
//$class = 'think\App';
//$prefix = 'think\';
if (isset(self::$prefixLengthsPsr4[$first])) {
foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach (self::$prefixDirsPsr4[$prefix] as $dir) {
//E:\xampp\htdocs\test\library\think路径前缀部分 + 文件分隔符
//+ think/App.php截取掉think/后余留下来的App.php
if (is_file($file = $dir . DS . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
这里注意2点:
1.这里prefix变量里反斜杠符号是在执行下面代码时加进去的。
self::addPsr4($prefix . '\\', rtrim($paths, DS), true);
加反斜杠的目的,是更加精确的确定命名空间,代表命名空间其中一段的终止。按如上代码逻辑,可防止与thinkme类似以think为前缀的命名空间发生混淆。
2.根据如上梳理总结寻找文件的主要思想:
文件位置 = 自定义命名空间对应的文件夹位置 + 文件分隔符 + 截取掉类的命名空间而余留下来的类名 + 文件后缀
以上梳理流程大大简化了thinkphp框架的自动加载机制,只选取了其中一种情况来进行剖析说明的。
日后有更多的理解,将会在此处进一步更新。
文中代码在github上有备份,欢迎下载。链接地址:https://github.com/bingodawson/tpautoload.git
thinkphp系列:类的自动加载是如何设计的的更多相关文章
- thinkphp5源码剖析系列1-类的自动加载机制
前言 tp5想必大家都不陌生,但是大部分人都停留在应用的层面,我将开启系列随笔,深入剖析tp5源码,以供大家顺利进阶.本章将从类的自动加载讲起,自动加载是tp框架的灵魂所在,也是成熟php框架的必备功 ...
- thinkphp学习笔记9—自动加载
原文:thinkphp学习笔记9-自动加载 1.命名空间自动加载 在3.2版本中不需要手动加载类库文件,可以很方便的完成自动加载. 系统可以根据类的命名空间自动定位到类库文件,例如定义了一个类Org\ ...
- Yaf零基础学习总结5-Yaf类的自动加载
Yaf零基础学习总结5-Yaf类的自动加载 框架的一个重要功能就是类的自动加载了,在第一个demo的时候我们就约定自己的项目的目录结构,框架就基于这个目录结构来自动加载需要的类文件. Yaf在自启动的 ...
- final关键字,类的自动加载,命名空间
final关键字 1.final可以修饰方法和类,但是不能修饰属性: 2.Final修饰的类不能被继承: 3.Fina修饰的方法不能被重写,子类可以对已被final修饰的父类进行访问,但是不能对父类的 ...
- PHP面向对象学习-属性 类常量 类的自动加载 构造函数和析构函数 访问控制(可见性)
在类的成员方法里面,可以用 ->(对象运算符):$this->property(其中 property 是该属性名)这种方式来访问非静态属性.静态属性则是用 ::(双冒号):self::$ ...
- PHP面向对象----- 类的自动加载
1.类的自动加载 spl_autoload_register函数 test.php <?php spl_autoload_register('autoload'); // require_onc ...
- 浅析PHP类的自动加载和命名空间
php是使用require(require_once)和include(include_once)关键字加载类文件.但是在实际的开发工程中我们基本上不会去使用这些关键字去加载类. 因为这样做会使得代码 ...
- ThinkPHP 3.2.3 自动加载公共函数文件的方法
方法一.加载默认的公共函数文件 在 ThinkPHP 3.2.3 中,默认的公共函数文件位于公共模块 ./Application/Common 下,访问所有的模块之前都会首先加载公共模块下面的配置文件 ...
- Drupal如何实现类的自动加载?
Drupal通过spl_autoload_register()注册类加载器实现自动加载: function _drupal_bootstrap_database() { // ... .... spl ...
随机推荐
- VS2010开发程序打包详解
VS2010开发程序打包详解 转自:http://blog.sina.com.cn/s/blog_473b385101019ufr.html 首先打开已经完成的工程,如图: 下面开始制作安装程序包. ...
- C#设计模式之十组合模式(Composite)【结构型】
一.引言 今天我们要讲[结构型]设计模式的第四个模式,该模式是[组合模式],英文名称是:Composite Pattern.当我们谈到这个模式的时候,有一个物件和这个模式很像,也符合这个模式要表达 ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Vue源码后记-vFor列表渲染(3)
这一节肯定能完! 经过DOM字符串的AST转化,再通过render变成vnode,最后就剩下patch到页面上了. render函数跑完应该是在这里: function mountComponent( ...
- 【JAVA零基础入门系列】Day12 Java类的简单应用
俗话说的好,实践出真知,所以除了理论知识掌握扎实以外,更重要的是要多加操练,这样才能掌握核心科技. 今天我们就用刚学会的类来实践一下,目标便是完成上一篇中的剁手任务. 我们的商品类已经准备好了,代码重 ...
- Cup
Cup Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- Flex 基础语法(二)
1.flex -direction 属性 含义 row(默认值) 主轴为水平方向,起点在左端. row-reverse 主轴为水平方向,起点在右边. column 主轴为垂直方向,起点在上沿. col ...
- python小小面试题
一.python是如何进行内存管理的?Python引入了一个机制:引用计数.python内部使用引用计数,来保持追踪内存中的对象,Python内部记录了对象有多少个引用,即引用计数,当对象被创建时就创 ...
- 一脚踏进Memcached的大门
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- docker下编译mangoszero WOW60级服务端(一)
这几天看到暴雪准备开放怀旧服的新闻,突然想到几年前用大芒果window一键服务端自己搭建过服务,就想着在Linux环境下重新编译一套,毕竟Linux作为服务端,性能和稳定性都会高一些,于是在mac虚拟 ...