主页
软件技术
返回
php主动加载

        php中有两种自动加载机制函数

        [php]

        __autoload();

        spl_autoload_register();

        1. __autoload()

        可以将需要使用类的时候把文件加载到程序中

         [php]

        <?php

        function __autoload($className) {

        if (file_exists($className . '.php')) {

        include $className . '.php';//可细化

        } else {

        echo $className . '.php is not exists.';

        exit;

        }

        }

        $indexController = new IndexController();

        在程序的运行过程中,php会检测这个$className类是否已经加载,如果没有加载会去执行__autoload(),再去加载$className这个类。在实例化类的对象、访问类中的静态变量和方法等都会去检测类是否已经加载,是否有定义__autoload()函数,如果都没有就会报错。

        在复杂点的系统中,用__autoload()来实现类的自动加载可能会很复

        杂。

        2. spl_autoload_register()

        [php]

        <?php

        spl_autoload_register();

        $index = new Index();

        spl_autoload_register()函数中没有参数,则会自动默认实现void

        spl_autoload ( string $class_name [,string $file_extensions ] )

        函数,默认支持.php和.ini

        [php]

        function load1($className) {

        //include

        }

        function load2($className) {

        //include

        }

        spl_autoload_register('load1');//注册到<span

        class="methodname">spl_autoload_functions</span>

        spl_autoload_register('load2');

        $index = new Index();

        会先通过load1去加载类,如果load1中没有,再通过load2去加载,如果还有以次类推。  

        实现一个自动加载方法比较多,这举例一个

        [php]

        <?php

        class autoloader {

        public static $loader;

        public static function init()

        {

        if (self::$loader == NULL)

        self::$loader = new self();

        return self::$loader;

        }

        public function __construct()

        {

        spl_autoload_register(array($this,'model'));

        spl_autoload_register(array($this,'helper'));

        spl_autoload_register(array($this,'controller'));

        spl_autoload_register(array($this,'library'));

        }

        public function library($class)

        {

        set_include_path(get_include_path().PATH_SEPARATOR.'/lib/');

        spl_autoload_extensions('.library.php');

        spl_autoload($class);

        }

        public function controller($class)

        {

        $class = preg_replace('/_controller$/ui','',$class);

        set_include_path(get_include_path().PATH_SEPARATOR.'/controller/');

        spl_autoload_extensions('.controller.php');

        spl_autoload($class);

        }

        public function model($class)

        {

        $class = preg_replace('/_model$/ui','',$class);

        set_include_path(get_include_path().PATH_SEPARATOR.'/model/');

        spl_autoload_extensions('.model.php');

        spl_autoload($class);

        }

        public function helper($class)

        {

        $class = preg_replace('/_helper$/ui','',$class);

        set_include_path(get_include_path().PATH_SEPARATOR.'/helper/');

        spl_autoload_extensions('.helper.php');

        spl_autoload($class);

        }

        }

        //call

        autoloader::init();

        ?>

        也可以根据自己的需要来设计实现

        


安全工程师辅导资料:低压线损的包保管理1
注册消防工程师难度大不大
邯郸市2015年造价工程师考试报名时间为6月10日至30日
2015年安全工程师生产事故案例分析精选(2)
施工项目付款流程明细规定 内容
时评:城镇化莫成强取豪夺游戏
08监理工程师《进度控制》练习题8
2015年安全工程师考试《管理知识》:预警控制
信息发布:名易软件http://www.myidp.net