PHP的autoLoad自动加载机制

PS:在php框架中都有自动加载的机制,autoload机制可以使得PHP程序有可能在使用类时才自动包含类文件,而不是一开始就将所有的类文件include进来,这种机制也称为lazy loading.下面文章在转发部分文章的前提下又写入了自己的对autoload机制的体会!

  1. autoload大致可以使用两种方法:__autoload和spl方法

  2. __autoload方法介绍

    1. __autoload的使用方法1:根据类名,找出类文件,然后require_one

                      function __autoload($class_name) {
                          $path = str_replace('_', '/', $class_name);
                          require_once $path . '.php';
                      }
                      // 这里会自动加载Http/File/Interface.php 文件
                      $a = new Http_File_Interface();
                      

      这种方法的好处就是简单易使用.当然也有缺点,缺点就是将类名和文件路径强制做了约定,当修改文件结构的时候,就势必要修改类名

    2. __autoload的使用方法2:直接映射法

                      function __autoload($class_name) {
                           $map = array(
                          'Http_File_Interface' => 'C:/PHP/HTTP/FILE/Interface.php'
                          );
                          if (isset($map[$class_name])) {
                              require_once $map[$class_name];
                          }
                      }
                      // 这里会自动加载C:/PHP/HTTP/FILE/Interface.php 文件
                      $a = new Http_File_Interface();
                      

      这种方法的好处就是类名和文件路径只是用一个映射来维护,所以当文件结构改变的时候,不需要修改类名,只需要将映射中对应的项修改就好了.

  3. __autoload和spl的应用场景

    __autoload在一个项目中只能使用一次,当你的项目引用了别人的一个项目,你的项目中有一个__autoload,别人的项目也有一个__autoload,这样两个__autoload就冲突了.解决的办法就是修改__autoload成为一个,这无疑是非常繁琐的,应用场景单一
    spl的autoload系列函数使用一个autoload调用堆栈,你可以使用spl_autoload_register注册多个自定义的autoload函数,应用场景广泛

            
  4. spl方法介绍

    1. spl的自动加载的相关函数

    2. spl_autoload 是_autoload()的默认实现,它会去include_path中寻找$class_name(.php/.inc) Spl_autoload实现自动加载:

                      void spl_autoload ( string $class_name [, string $file_extensions ] )
                      #在默认情况下,本函数先将类名转换成小写,再在小写的类名后加上 .inc 或 .php 的扩展名作为文件名,然后在所有的包含路径(include paths)中检查是否存在该文件。
                      
                      /*/home/www/http.php*/
                      class http {
                          public function callname(){
                              echo "this is http";
                          }
                      }
      
                      /*/home/www/test.php*/
                      set_include_path("/home/www/"); //这里需要将路径放入include
                      spl_autoload("http",'.ini'); //寻找/home/www/http.php
                      $a = new http();
                      $a->callname();
                      
    3. Spl_autoload_register将函数注册到SPL __autoload函数栈中

                      bool spl_autoload_register ([ callback $autoload_function ] )
                      
                      /*/home/www/http.php*/
                      class http {
                          public function callname(){
                              echo "this is http";
                          }
                      }
                      /*/home/www/test.php*/
                      function AutoLoad($class){
                          if($class == 'http'){
                              require_once("/home/www/http.php");
                          }
                      }
                      spl_autoload_register('AutoLoad');
                      $a = new http();
                      $a->callname();
                      
    4. spl_autoload_call

                      /*/home/www/http.php*/
                      class http {
                          public function callname(){
                              echo "this is http";
                          }
                      }
                      /*/home/www/http2.php*/
                      class http {
                          public function callname(){
                              echo "this is http2";
                          }
                      }
                      /*/home/www/test.php*/
                      function AutoLoad($class){
                          if($class == 'http'){
                              require_once("/home/www/http.php");
                          }elseif($class == 'http2'){
                              require_once("/home/www/http2.php");
                          }
                      }
                      spl_autoload_register('AutoLoad');
                      spl_autoload_call('http2');//调用所有已注册的__autoload()函数来加载请求参数$class为'http2'的文件
                      $a = new http();
                      $a->callname(); //这个时候会输出"this is http2"
                      
  5. spl_autoload_register注册多个自定义的autoload函数的应用

    重点关注理解

    1. spl_autoload_register注册多个自定义函数

                      /*/home/www/http.php*/
                      class http {
                          public function callname(){
                              echo "this is http";
                          }
                      }
                      /*/home/www/http2.php*/
                      class http {
                          public function callname(){
                              echo "this is http2";
                          }
                      }
                      /*/home/www/test.php*/
                      function AutoLoad1($class){
                          if($class == 'http'){
                              require_once("/home/www/http.php");
                          }
                      }
                      function AutoLoad2($class){
                          if($class == 'http2'){
                              require_once("/home/www/http2.php");
                          }
                      }
                      spl_autoload_register('AutoLoad2');
                      spl_autoload_register('AutoLoad1');//当AutoLoad2()找不到时,我来找
                      $a = new http();
                      $a->callname(); //这个时候会输出"this is http2"
                      
    2. spl_autoload_register注册多个自定义类方法

                      /*/home/www/http.php*/
                      class http {
                          public function callname(){
                              echo "this is http";
                          }
                      }
                      /*/home/www/http2.php*/
                      class http2 {
                          public function callname(){
                              echo "this is http2";
                          }
                      }
                      /*/home/www/test.php*/
                      class Loader {
                          /**
                          * 自动加载类
                          * @param $class 类名
                          */
                          public static function autoload1($class) {
                              if($class == 'http'){
                                  require_once("/home/www/http.php");
                              }
                          }
                          public static function autoload2($class) {
                              if($class == 'http2'){
                                  require_once("/home/www/http2.php");
                              }
                          }
                      }
                      spl_autoload_register(array('Loader', 'autoload1'));//自己写一些加载的代码
                      spl_autoload_register(array('Loader', 'autoload2'));//当autoload1()找不到时,我来找
                      $a = new http2();
                      $a->callname(); //这个时候会输出"this is http2"
                      

总结:
1:总结内容
2:总结内容
3:总结内容


参考:
1.说说PHP的autoLoad自动加载机制
2.php自动加载
3.PHP自动加载__autoload的工作机制

相关文章
  1. $_GET,$_POST与urldecode的使用风险
  2. yii2框架的错误处理
  3. Yii2过滤器-behaviors()行为调用
  4. 密码保护:PHP多进程编程
  5. 全排列算法原理和实现
  6. PHP静态方法和非静态方法的使用场景
本站版权
1、本站所有主题由该文章作者发表,该文章作者与尘埃享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和尘埃的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:
二维码
Posted in php, 命名空间, 编程语言

发表评论