9)添加自动加载函数

注意:我把之前的控制器文件名都加了C,不然,那个自动记载函数,我不会写

目录雏形:

      

我开始把自动加载类放在index.php代码的最下面,然后就报了这个错误:

  

因为我把代码中只要是类加载的地方,全部都注释了,所以,所以他报了--------Controller这个类没加载到,哎

气死我了,然后我发现,那个自动加载函数根本没进去,因为我加了这行代码,根本没有输出出来:

后面,我才发现,原来这个index.php代码在前面的第11行发现了错误,那么后面可定就不运行了啊,哎,真是敲得代码少,这样的问题都不知道》》。。。。。。

我的新的index.php代码展示:

    

 1 <?php
 2     /**
 3      * Created by PhpStorm.
 4      * User: Interact
 5      * Date: 2017/8/19
 6      * Time: 22:02
 7      */
 8     //确定分发参数
 9     //动作
10     define('CONTROLLER',isset($_GET['c'])?$_GET['c']:'zixunC');
11     define('ACTION',isset($_GET['a'])?$_GET['a']:'show');
12     define("PLATFORM",isset($_GET['p'])?$_GET['p']:'test');
13     function userautoload($class_name){
14         
15 //        var_dump($class_name);
16         //先处理确定的(框架中的核心类)
17         // 类名与类文件映射数组
18         $framework_class_list = array(
19             // '类名' => '类文件地址'
20             'Controller' => './framework/Controller.php',
21             'Model' => './framework/Model.class.php',
22             'Factory' => './framework/Factory.class.php',
23             'MySQLDB' => './framework/MySQLDB.class.php',
24         ) ;
25 //        echo "走没走";
26         //判断是否为核心类
27         if (isset($framework_class_list[$class_name])) {
28             //是核心类
29             require $framework_class_list[$class_name];
30         }
31         //判断是否为可增加(控制器类,模型类)
32         //控制器类,截取后是个字符,匹配Controller
33         elseif (substr($class_name, -1) == 'C') {
34             // 控制器类, 当前平台下controller目录
35             require './application/' . PLATFORM . '/controller/' . $class_name . '.controller.class.php';
36         }
37         //模型类,截取后5个字符,匹配Model
38         elseif (substr($class_name, -5) == 'Model') {
39             // 模型类,当前平台下model目录
40             require './application/' . PLATFORM . '/model/' . $class_name . '.class.php';
41         }
42         
43     }
44     spl_autoload_register('userautoload');
45     
46     
47     
48     
49     require './application/'.PLATFORM.'/controller/'.CONTROLLER.'.controller.class.php';
50 $controlelr_name=CONTROLLER;
51 $controller=new $controlelr_name();
52 $action_name=ACTION;
53 $controller->$action_name();

注意我的自动加载函数放到了后面类的实例化的前面。

html代码有改动:

    

html代码展示:

     

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="utf-8"><!-- 编码格式是 utf-8 -->
 5     <meta http-equiv="X-UA-Compatible" content="IE=edge"><!-- 使用最新式 ie 内核渲染国内某些 所谓的 双核浏览器 或者是 直接 使用webkit去渲染-->
 6     <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
 7     <!-- 视口属性没有设置 禁用 用户缩放, 如果有需求可以添加-->
 8     <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
 9     <title>bootstrap的模板页</title>
10     <!-- Bootstrap -->
11 
12 </head>
13 <body>
14 <table>
15     <tr>
16        <th>ID&nbsp&nbsp&nbsp</th>
17         <th>名字&nbsp&nbsp&nbsp</th>
18         <th>分类&nbsp&nbsp&nbsp</th>
19         <th>作者&nbsp&nbsp&nbsp</th>
20         <th>更新时间&nbsp&nbsp</th>
21         <th>浏览次数&nbsp&nbsp</th>
22         <th>发布状态&nbsp&nbsp</th>
23     </tr>
24     <?php foreach($records as $row) : ?>
25     <tr>
26         <th><?php echo $row['ZX_id']; ?></th>
27         <th><?php echo $row['ZX_name']; ?></th>
28         <th><?php echo $row['ZX_fenlei']; ?></th>
29         <th><?php echo $row['ZX_zuozhe']; ?></th>
30         <th><?php echo $row['gengxin_time']; ?></th>
31         <th><?php echo $row['liulan_cishu']; ?></th>
32         <th><?php echo $row['fabu_zhuangtai']; ?></th>
33         <th><a href="index.php?a=delete">删除</a></th>
34         <th><a href="index.php?c=newnewnewC&a=hanshu">新的页面展示</a></th>
35     </tr>
36     <?php endforeach ?>
37 </table>
38 </body>
39 </html>

  注释掉类加载的地方:

    

    

    

结果展示:

    


      

原文地址:https://www.cnblogs.com/xiaoyoucai/p/7399721.html