略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: spl_autoload_functions

2024-04-27

spl_autoload_functions

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

spl_autoload_functions返回所有已注册的 __autoload() 函数

说明

spl_autoload_functions(): array

获取所有已注册的 __autoload() 函数。

参数

此函数没有参数。

返回值

包含所有已注册的 __autoload 函数的数组(array)。如果自动装载函数队列未激活,则返回 false。如果没有已注册的函数,则返回一个空数组。

add a noteadd a note

User Contributed Notes 2 notes

up
5
dantedantas at gmail dot com
4 years ago
If you use an anonymous function, it will return the object that are expected.

spl_autoload_register(function ($myclass){
    $keyclass = substr($myclass, 0, 1);

    switch ($keyclass) {
        case 'c':
            if (file_exists("class".DIRECTORY_SEPARATOR.$myclass.".php") === true)
                require_once ("class".DIRECTORY_SEPARATOR.$myclass.".php");
            break;
        case 'i':
            if (file_exists("interface".DIRECTORY_SEPARATOR.$myclass.".php") === true)
                require_once ("interface".DIRECTORY_SEPARATOR.$myclass.".php");
            break;
        case 'a':
            if (file_exists("abstract".DIRECTORY_SEPARATOR.$myclass.".php") === true)
                require_once ("abstract".DIRECTORY_SEPARATOR.$myclass.".php");
            break;
        default:
            if (file_exists($myclass.".php") === true)
                require_once ($myclass.".php");
    }

/******************************/

var_dump(spl_autoload_functions()) return:

array(1) {
  [0]=>
  object(Closure)#1 (1) {
    ["parameter"]=>
    array(1) {
      ["$myclass"]=>
      string(10) "<required>"
    }
  }
}
up
1
124307954 at qq dot com
3 years ago
<?php
spl_autoload_register
(function ($name) {
    echo
"Want to load $name.\n";
  
});

spl_autoload_register(function($className) {
   
var_dump($className);
});

function
unregister($className) {
   
var_dump($className.' i will be the first');
}

spl_autoload_register('unregister');

var_dump(spl_autoload_functions());

===================

array(
3) {
  [
0]=>
 
object(Closure)#1 (1) {
   
["parameter"]=>
    array(
1) {
      [
"$name"]=>
     
string(10) "<required>"
   
}
  }
  [
1]=>
 
object(Closure)#2 (1) {
   
["parameter"]=>
    array(
1) {
      [
"$className"]=>
     
string(10) "<required>"
   
}
  }
  [
2]=>
 
string(10) "unregister"
}

官方地址:https://www.php.net/manual/en/function.spl-autoload-functions.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3