PHP自动加载机制概述
本内容深入剖析了PHP中spl_autoload_register()函数的具体应用方式,旨在为开发者提供实用的参考代码与解析。
在探讨该核心函数之前,有必要先回顾另一个相关的自动加载机制:__autoload。
一、__autoload 机制概述
__autoload是一个内置的自动加载函数。在PHP5环境中,当程序尝试实例化一个尚未被定义的类时,系统会自动触发此函数以尝试加载该类文件。以下代码演示了这一过程:
printit.class.php:
<?php
class PRINTIT {
function doPrint() {
echo 'hello world';
}
}
?>
index.php
<?
function __autoload( $class ) {
$file = $class . '.class.php';
if ( is_file($file) ) {
require_once($file);
}
}
$obj = new PRINTIT();
$obj->doPrint();?>
执行index.php后,控制台正常输出“hello world”。尽管index.php文件中并未显式包含printit.class.php,但在实例化printit对象时,系统自动调用了__autoload函数。此时,参数$class的值被赋值为类名“printit”,从而成功引入了对应的类文件。
这种自动加载策略在面向对象编程中极为常见,它能有效减少手动引用文件的代码量,显著提升系统的灵活性与可维护性。
二、spl_autoload_register() 函数解析
接下来介绍spl_autoload_register(),它与__autoload有着异曲同工之妙,但提供了更强大的控制能力。请看以下简化示例:
<?
function loadprint( $class ) {
$file = $class . '.class.php';
if (is_file($file)) {
require_once($file);
}
}
spl_autoload_register( 'loadprint' );
$obj = new PRINTIT();
$obj->doPrint();?>
此处将默认的__autoload替换为自定义的loadprint函数。然而,loadprint不会像__autoload那样由系统自动触发。此时,spl_autoload_register()便发挥了关键作用,它指示PHP引擎:一旦遇到未定义的类,即执行loadprint()函数进行加载处理。
三、spl_autoload_register() 调用静态方法
<?
class test {
public static function loadprint( $class ) {
$file = $class . '.class.php';
if (is_file($file)) {
require_once($file);
}
}
}
spl_autoload_register( array('test','loadprint') );
//另一种写法:spl_autoload_register( "test::loadprint" );
$obj = new PRINTIT();
$obj->doPrint();?>
函数名称:spl_autoload_register
适用版本:(PHP 5 >= 5.1.2)
功能描述:spl_autoload_register — 注册__autoload()函数
语法说明
bool spl_autoload_register ([ callback $autoload_function ] ) 该函数用于将指定函数注册到SPL的__autoload函数栈中。如果栈中的函数尚未激活,系统将自动激活它们。
若程序中已存在__autoload函数的实现,必须将其显式注册到__autoload栈中。这是因为spl_autoload_register()函数会将Zend Engine中的__autoload函数指针替换为spl_autoload() 或 spl_autoload_call()。
参数说明
autoload_function
需要注册的自动装载回调函数。若未提供参数,则默认注册autoload的标准实现函数spl_autoload()。
返回值说明
注册成功返回 TRUE,失败则返回 FALSE。
注:SPL全称为Standard PHP Library(标准PHP库),是PHP5引入的一个扩展库。其主要功能涵盖autoload机制的实现以及各种Iterator接口或类的支持。SPL autoload机制的核心在于将函数指针autoload_func指向其自身实现的具有自动装载功能的函数。SPL提供了两个不同的函数spl_autoload和spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现差异化的自动加载行为。
classLOAD
{
staticfunctionloadClass($class_name)
{
$filename= $class_name.".class.php";
$path= "include/".$filename
if(is_file($path)) returninclude$path;
}
}
/**
* 设置对象的自动载入
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array('LOAD', 'loadClass'));
/**
*__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
* 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
*/
spl_autoload_register( '__autoload');
如果使用spl_autoload_register同时注册了一个类的方法和一个__autoload函数,执行顺序取决于注册的先后次序。若第一个注册的方法或函数已成功加载类文件,则后续注册的第二个方法或函数将不再执行;反之,若第一个未能加载,则会继续执行第二个被注册的类的方法或函数。
<?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 ();
?>
希望上述关于spl_autoload_register()函数的解析与实例,能对各位的PHP程序设计工作有所帮助。