php自动加载_PHP教程

WBOY
Freigeben: 2016-07-14 10:07:50
Original
765 Leute haben es durchsucht

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

 
[php]  
__autoload();  
  
spl_autoload_register();  
1. __autoload()
 
可以将需要使用类的时候把文件加载到程序中
 
[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]  
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');//注册到spl_autoload_functions  
spl_autoload_register('load2');  
  
$index = new Index();  
会先通过load1去加载类,如果load1中没有,再通过load2去加载,如果还有以次类推。
 
实现一个自动加载方法比较多,这举例一个
 
 
 
[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...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!