php自动加载机制mod,php自动加载 - php开源社区的个人空间 - OSCHINA - 中文开源技术交流社区...

1fec26f190623b5f79de3335a9c12dbf.png

PHP实现自动加载,有两种方法:

①魔术函数 __autoload()

②spl扩展 spl_autoload_register

分别举例说明:

一、__autoload

printit.class.php:

1 <?php

2 class PRINTIT {

3 function doPrint() {

4 echo 'hello world';

5 }

6 }

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类内的方法。

在index.php中,由于没有包含printit.class.php,在实例化printit时,自动调用__autoload函数,参数$class的值即为类名printit,此时printit.class.php就被引进来了。

在面向对象中这种方法经常使用,可以避免书写过多的引用文件,同时也使整个系统更加灵活。

二、spl_autoload_register()

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是Standard PHP Library(标准PHP库)的缩写。

它是PHP5引入的一个扩展库,其主要功能包括autoload机制的实现及包括各种Iterator接口或类。

SPL autoload机制的实现是通过将函数指针autoload_func指向自己实现的具有自动装载功能的函数来实现的。

SPL有两个不同的函数spl_autoload, spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制。

classLoad

{

function static loadClass($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函数,那么,会根据注册的先后,如果在第一个注册的方法或函数里加载了类文件,就不会再执行第二个被注册的类的方法或函数。

反之就会执行第二个被注册的类的方法或函数。

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 ();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值