zoukankan      html  css  js  c++  java
  • 自定加载的简单实例

    如果想要加载一个目录下的多个类文件,并且含有一定的特征。

    如:D:/AppServ/www/myExercise/下

    新建了几个文件夹和文件

    文件夹mods->inmod.mod.php

          ->oumod.mod.php

    文件夹libs->inlib.lib.php

    类名与文件有一定的关联,这儿的类名与文件的第一个.号前相同,后面都最为后缀。

    inmod.mod.php中的代码:

    <?php
    class inmod {

      function __construct() {
        echo '我是mods下的in';
      }

      function test() {
        echo 'inmod test';
      }

    }

    oumod.mod.php中的代码:

    <?php
    class oumod {

      function __construct() {
        echo '我是mods下的ou';
      }

      function test() {
        echo 'oumod test';
      }

    }

    inlib.lib.php中的代码:

    <?php
    class inlib {

      function __construct() {
        echo '我是libs下的in';
      }

    }

    然后在auto_load文件加下新建一个类,load.php

    代码如下:

    <?php
    class Loader {

      /**
      * 自动加载类
      * @param $class 类名
      */
      public static function mods($class) {
        if ($class) {
          set_include_path('D:/AppServ/www/myExercise/auto_load/mods');
          spl_autoload_extensions('.mod.php');
          spl_autoload(strtolower($class));
        }
      }

      public static function libs($class) {
        if ($class) {
          set_include_path('D:/AppServ/www/myExercise/auto_load/libs');
          spl_autoload_extensions('.lib.php');
          spl_autoload(strtolower($class));
        }
      }

    }

    spl_autoload_register(array('Loader', 'mods')); // 注册加载Loader类下的mods方法从而促发spl_autoload函数调用
    spl_autoload_register(array('Loader', 'libs'));

    新建一个test.php文件:

    <?php
    require 'loader.php';

    //$inmoad = new inmod();

    inmod::test();
    echo '<br>';
    oumod::test();

    不会报错,仍然会输出类中的结果,说明类已经加载进来了。

    结果如下:

    inmod test
    oumod test

  • 相关阅读:
    ubuntu下按安装lamp
    linux 一些简记
    编写shell脚本步骤
    C++ array vector 数组
    抓取网页扒图片相对路径改绝对路径
    静态html文件js读取url参数
    IE7的web标准之道——1:前言(兼目录) (很牛的CSS书籍)
    感谢放逐自由《博客园精华集》分类索引
    这篇文章证实了索引对于IN,LIKE的优化程度,顺便学会了怎么看看语耗费的时间
    命名空间“System”中不存在类型或命名空间名称“Linq”(是缺少程序集引用吗?)
  • 原文地址:https://www.cnblogs.com/xingmeng/p/3161169.html
Copyright © 2011-2022 走看看