Home Backend Development PHP Tutorial Summary of usage of spl_autoload_register function in PHP_PHP tutorial

Summary of usage of spl_autoload_register function in PHP_PHP tutorial

Jul 13, 2016 am 10:25 AM
autoload php sp

spl_autoload_register
(PHP 5 >= 5.1.2)
spl_autoload_register — Register __autoload() function

Description
bool spl_autoload_register ([ callback $autoload_function ] )
Register the function into the SPL __autoload function stack. Activate functions in this stack if they are not already active.
If the __autoload function has been implemented in your program, it must be explicitly registered in the __autoload stack. Because the
spl_autoload_register() function will replace the __autoload function in Zend Engine with spl_autoload() or
spl_autoload_call().
Parameters
autoload_function
The autoload function to be registered. If no parameters are provided, the default implementation function
spl_autoload() of autoload is automatically registered.
Return Value
Returns TRUE if successful, FALSE if failed.

Note: SPL is the abbreviation of Standard PHP Library. It is an extension library introduced in PHP5. Its main functions include the implementation of the autoload mechanism and various Iterator interfaces or classes. The SPL autoload mechanism is implemented by pointing the function pointer autoload_func to a self-implemented function with autoloading function. SPL has two different functions, spl_autoload and spl_autoload_call. Different automatic loading mechanisms are implemented by pointing autoload_func to these two different function addresses.

Example
Suppose we have a class file A.php, which defines a class named A:
class A
{
public function __construct()
{
echo 'Got it.';
}
}

class A
{
public function __construct()
{
echo 'Got it.';
}
}
Then We have an index.php that needs to use this class A. The conventional writing method is
require('A.php');
$a = new A();

require('A.php');
$a = new A();

But one problem is that if our index.php needs to contain not just class A, but many classes, then we must write many lines of require statements, which sometimes makes people feel uncomfortable.

But in versions after php5, we no longer need to do this. In php5, the __autoload function is automatically called when trying to use a class that has not been defined, so we can write the __autoload function to let php automatically load the class without having to write a long list of include files.

For example, in the above example, index.php can be written like this:

function __autoload($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once ($file);
}
}

$a = new A();

function __autoload($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once ($file);
}
}

$a = new A();
Of course the above is just the simplest demonstration. __autoload just goes to include_path to find the class file and loads it. We can define the rules for __autoload to load classes according to our own needs.

In addition, if we do not want to call __autoload when automatically loading, but call our own function (or class method), we can use spl_autoload_register to register our own autoload function. Its function prototype is as follows:
bool spl_autoload_register ( [callback $autoload_function] )

Let’s continue to rewrite the above example:

function loader($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once ($file);
}
}

spl_autoload_register('loader');

$a = new A();

function loader($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once ($file);
}
}

spl_autoload_register('loader');

$a = new A();
This can also run normally. At this time, when PHP is looking for a class, it does not call __autoload but calls our own defined function loader. For the same reason, the following writing method is also possible:
class Loader
{
public static function loadClass($class)
{
$file = $ class . '.php';
if (is_file($file)) {
require_once($file);
}
}
}

spl_autoload_register(array('Loader', 'loadClass'));

$a = new A();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825084.htmlTechArticlespl_autoload_register (PHP 5 = 5.1.2) spl_autoload_register — Register __autoload() function description bool spl_autoload_register ([ callback $ autoload_function ] ) Register the function to SPL...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles