寻常php的加载是通过include(),require()等方法来加载外部文件,之后再通过实例调用方法或直接调用静态方法,而这样子写引入语句实在很麻烦,有的框架会将特定路径的文件全部引入,直接实例化就能使用,但这样一来有的类包不一定用到,写的类包越多的时候,加载的东西就不少了,影响程序的性能。

通过PHP的反射类 ReflectionClass 可以直接获得对应类的一个反射类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//test.php:
<?php
 class test{
     public function showName(){
         var_dump(__CLASS__);
     }
 }
?>
//index.php:
<?php
 var_dump(get_included_files()); 
 $rf new ReflectionClass('test');
 var_dump(get_included_files());
 
 $testObj $rf->newInstance();
 $testObj->showName();
 
 function __autoload($classname){
  
    $classpath './' $classname '.php';
    if (file_exists($classpath)) {
        require_once($classpath);
    }else {
        echo 'class file'.$classpath.'not found!';
    }
 }
 ?>
  //array
  // 0 => string 'D:\code\www\test\index.php' (length=26)
  //array
  // 0 => string 'D:\code\www\test\index.php' (length=26)
  // 1 => string 'D:\code\www\text\test.php' (length=25)
  //string 'test' (length=4)

实例化一个 ReflectionClass,并传类名进去,就会得到一个对应类的反射类。用实例调用 newInstance就会得到反射类的实例,这样就完成了实例化。

通过 get_included_files() 方法,我们可以看到当前页面引入的文件。在实例化反射类前,只有index.php文件,实例化反射类后,自动引入了一个test.php文件,那么看下上面那段代码,发现有个__autoload()名字的魔术方法,这方法就定义了自动加载文件,而ReflectionClass在当前页面找不到类时,就会调用__autoload()去加载类。这就是自动加载的过程。

想知道__autoload()方法有没有开启,可以通过PHP的标准库SPL中的方法来查看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var_dump(spl_autoload_functions());
spl_autoload_register('newAutoload');
var_dump(spl_autoload_functions());
 
$testObj1 = getInstance('test');
$testObj2 = getInstance('test');
$testObj3 = getInstance('test');
 
function getInstance($class$returnInstance = false){
    $rf new ReflectionClass($class);
    if ($returnInstance)
         return $rf->newInstance();
}
 
function newAutoload($classname){
 
    $classpath './' $classname '.php';
    if (file_exists($classpath)) {
        var_dump('require success');
        require_once($classpath);
    else {
        echo 'class file ' $classpath ' not found!';
    }
}
//array
//  0 => string '__autoload' (length=10)
//array
//  0 => string 'newAutoload' (length=11)
//string 'require success' (length=15)

spl_autoload_functions() 方法是用来查看当前自动加载的方法,当前有个__autoload魔术方法,所以返回了函数名,若没定义自动加载方法的话,返回的是false,而 spl_autoload_register() 方法是通过方法名将一个方法注册到自动加载方法,这里用newAutoload方法来替换__autoload方法。

newAutoload方法中,每执行成功一次,打印一句'require success',这里只打印了一次,说明了虽然实例了3次ReflectionClass('test'),但因为test类已经加载过一次,就不会再执行自动加载的方法。通过getInstance()这种加载类的方法,比以前的include()之类的方便多了,只需要加载这个写了getInstance()方法的文件就可以了。

重写的自动加载方法可以根据需要,通过判断类的名字,定义不同的文件路径。getInstance可以用静态变量保存实例,这也是使用到了设计模式中的单例模式。

http://my.oschina.net/jiec/blog/361639?p={{currentPage+1}}

最新文章

  1. C#去掉HTML标记
  2. Ecshop商品促销时间精确到小时分钟和秒的设置方法 调用时间
  3. [LeetCode] Remove Duplicates from Sorted List
  4. Run_Product Example Form - Oracle Forms 6i
  5. MFC应用程序框架(转)
  6. ISO中AFN的使用步骤
  7. 原生js判断css3动画过度(transition)结束 transitionend事件 以及关键帧keyframes动画结束(animation)回调函数 animationEnd 以及 css 过渡 transition无效
  8. 支持异步同步的分布式CommandBus MSMQ实现
  9. 如何在标题栏的title前添加网站logo
  10. oracle登录时shared memory realm does not exist的解决方法
  11. MySQL大数据量分页查询
  12. 11.Java 加解密技术系列之 总结
  13. EasyUI 二次加载toolbar异常问题解决
  14. [WP]BugkuCtf - pwn2
  15. Treap仿set 模板
  16. SQL里执行CLR c#代码
  17. 【代码笔记】iOS-UIActionSheet字体的修改
  18. 记录第一次使用jni编译so包的入门操作
  19. 第三百四十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制
  20. Flash数据的采集方法-搜房房价走势采集

热门文章

  1. IE测试CSS兼容性测试
  2. 新闻焦点切换flash应用
  3. UIImagePickerController 视频录制操作,视频大小,时间长度
  4. webpack+vuecli打包常见的2个坑
  5. onethink 密码加密方式详解
  6. ant font 本地化
  7. 【PPT】PPT倒计时动画的制作方法 5.4.3.2.1...
  8. 英特尔 Android* 开发者指南上的对等应用
  9. Github 入门基本操作
  10. MyEclipse 注释乱码问题