关于PHP中spl_autoload_register()函数用法详解

墨辰丷
Lepaskan: 2023-03-29 10:22:01
asal
12998 orang telah melayarinya

这篇文章主要介绍了PHP中spl_autoload_register()函数用法,结合实例形式分析了__autoload函数及spl_autoload_register函数的相关使用技巧,需要的朋友可以参考下

推荐手册php完全自学手册

在了解这个函数之前先来看另一个函数:__autoload。

一、__autoload

这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数。看下面例子:

printit.class.php:

<?php
class PRINTIT {
 function doPrint() {
 echo &#39;hello world&#39;;
 }
}
?>
Salin selepas log masuk

index.php

<?
function __autoload( $class ) {
 $file = $class . &#39;.class.php&#39;;
 if ( is_file($file) ) {
 require_once($file);
 }
}
$obj = new PRINTIT();
$obj->doPrint();?>
Salin selepas log masuk

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

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

二、spl_autoload_register()

再看spl_autoload_register(),这个函数与__autoload有与曲同工之妙,看个简单的例子:

<?
function loadprint( $class ) {
 $file = $class . &#39;.class.php&#39;;
 if (is_file($file)) {
 require_once($file);
 }
}
spl_autoload_register( &#39;loadprint&#39; );
$obj = new PRINTIT();
$obj->doPrint();?>
Salin selepas log masuk

将__autoload换成loadprint函数。但是loadprint不会像__autoload自动触发,这时spl_autoload_register()就起作用了,它告诉PHP碰到没有定义的类就执行loadprint()。

spl_autoload_register() 调用静态方法

<?
class test {
 public static function loadprint( $class ) {
 $file = $class . &#39;.class.php&#39;;
 if (is_file($file)) {
  require_once($file);
 }
 }
}
spl_autoload_register( array(&#39;test&#39;,&#39;loadprint&#39;) );
//另一种写法:spl_autoload_register( "test::loadprint" );
$obj = new PRINTIT();
$obj->doPrint();?>
Salin selepas log masuk

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(&#39;LOAD&#39;, &#39;loadClass&#39;));
/**
*__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
* 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
*/
spl_autoload_register( &#39;__autoload&#39;);
Salin selepas log masuk

如果同时用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, &#39;model&#39; ) );
    spl_autoload_register ( array ($this, &#39;helper&#39; ) );
    spl_autoload_register ( array ($this, &#39;controller&#39; ) );
    spl_autoload_register ( array ($this, &#39;library&#39; ) );
  }
  public function library($class) {
    set_include_path ( get_include_path () . PATH_SEPARATOR . &#39;/lib/&#39; );
    spl_autoload_extensions ( &#39;.library.php&#39; );
    spl_autoload ( $class );
  }
  public function controller($class) {
    $class = preg_replace ( &#39;/_controller$/ui&#39;, &#39;&#39;, $class );
    set_include_path ( get_include_path () . PATH_SEPARATOR . &#39;/controller/&#39; );
    spl_autoload_extensions ( &#39;.controller.php&#39; );
    spl_autoload ( $class );
  }
  public function model($class) {
    $class = preg_replace ( &#39;/_model$/ui&#39;, &#39;&#39;, $class );
    set_include_path ( get_include_path () . PATH_SEPARATOR . &#39;/model/&#39; );
    spl_autoload_extensions ( &#39;.model.php&#39; );
    spl_autoload ( $class );
  }
  public function helper($class) {
    $class = preg_replace ( &#39;/_helper$/ui&#39;, &#39;&#39;, $class );
    set_include_path ( get_include_path () . PATH_SEPARATOR . &#39;/helper/&#39; );
    spl_autoload_extensions ( &#39;.helper.php&#39; );
    spl_autoload ( $class );
  }
}
//call
autoloader::init ();
?>
Salin selepas log masuk

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关文章推荐:           
1.PHP自动加载机制介绍——spl_autoload_register()函数,php类自动加载
2.如何使用spl_autoload_register实现自动加载实例详解
相关视频推荐:
1.独孤九贱(4)_PHP视频教程

Atas ialah kandungan terperinci 关于PHP中spl_autoload_register()函数用法详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!