php automatic loading_PHP tutorial

WBOY
Release: 2016-07-14 10:07:50
Original
790 people have browsed it

There are two automatic loading mechanism functions in php

[php]
__autoload();
spl_autoload_register();
1. __autoload()
You can load the file into the program when you need to use the class
[php]
function __autoload($className) {
if (file_exists($className . '.php')) {
include $className . '.php';//can be refined
} else {
echo $className . '.php is not exists.';
exit;
}
}
$indexController = new IndexController();
During the running of the program, PHP will detect whether the $className class has been loaded. If it is not loaded, it will execute __autoload() and then load the $className class. When instantiating class objects, accessing static variables and methods in the class, etc., they will check whether the class has been loaded and whether the __autoload() function is defined. If not, an error will be reported.
In more complex systems, using __autoload() to implement automatic loading of classes may be complicated.
2. spl_autoload_register()
[php]
spl_autoload_register();
$index = new Index();
There are no parameters in the spl_autoload_register() function, and the void spl_autoload (string $class_name [,string $file_extensions]) function will be automatically implemented by default. It supports .php and .ini by default
[php]
function load1($className) {
//include
}
function load2($className) {
//include
}
spl_autoload_register('load1');//Register tospl_autoload_functions
spl_autoload_register('load2');
$index = new Index();
The class will be loaded through load1 first. If there is no class in load1, it will be loaded through load2, and so on.
There are many ways to implement automatic loading. Here is an example
[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();  
?>  
也可以根据自己的需要来设计实现

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477831.htmlTechArticlephp中有两种自动加载机制函数 [php] __autoload(); spl_autoload_register(); 1. __autoload() 可以将需要使用类的时候把文件加载到程序中 [php] ?php function...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!