Current Location: Home> Function Categories> spl_autoload_extensions

spl_autoload_extensions

Register and return the default file extension used by the spl_autoload function
Name:spl_autoload_extensions
Category:SPL
Programming Language:php
One-line Description:Set or get the file extension for automatically loading classes

Function name: spl_autoload_extensions()

Applicable version: PHP 5 >= 5.1.0, PHP 7

Function description: The spl_autoload_extensions() function is used to set or get the file extension used to automatically load the class.

usage:

  1. Get the file extension of the currently set autoloading class:

     $extensions = spl_autoload_extensions(); echo $extensions;
  2. Set the file extension for the automatic loading class:

     spl_autoload_extensions(".php,.inc");

Example: Suppose we have the following directory structure:

  • classes/
    • MyClass.php
    • OtherClass.inc
  • index.php

Now we want to automatically load these class files. We can use the spl_autoload_extensions() function to set the autoloaded file extension to ".php,.inc", and then use the spl_autoload_register() function to register a custom autoload function.

 // 设置自动加载类的文件扩展名spl_autoload_extensions(".php,.inc"); // 自定义自动加载函数function myAutoload($className) { $filename = __DIR__ . '/classes/' . $className . '.php'; if (file_exists($filename)) { include $filename; } } // 注册自动加载函数spl_autoload_register('myAutoload'); // 创建一个MyClass对象$obj = new MyClass();

In the example above, we first use spl_autoload_extensions() to set the autoloaded file extension to ".php,.inc". Then an automatic loading function named myAutoload is defined, which will dynamically load the corresponding class file according to the class name. Finally, we register the myAutoload function as an autoload function through the spl_autoload_register() function. When we create a MyClass object, the automatic loading function will automatically load the MyClass.php file.

Note: The file extension set by spl_autoload_extensions() is global and will affect all autoloading functions.

Similar Functions
Popular Articles