Table of Contents
Register other autoloading functions
Register automatic loading method
Registration of static methods
Registration of non-static methods
Home Backend Development PHP Tutorial PHP implements automatic loading of related functions

PHP implements automatic loading of related functions

Mar 17, 2018 am 09:52 AM
php function Related

When PHP is executed, if it is found that a class needs to be used (as long as it is any operation related to this class), at this time, the current script does not have the definition code of this class! Then PHP will automatically call a function named __autoload() at this time! And, when calling, a parameter will be passed to the function, which is the class name of the currently required class.

#自动加载 函数function __aotuload($class_name){
    if(!file_exists("./".$class_name.".class.php")){
        include "./".$class_name.".class.php";
    }else{
        die("系统错误,没有找到相关的类!");
    }
}
Copy after login

Register other autoloading functions

Generally, the system’s autoloading function is __autoload(). However, as the project expands, multiple autoloading functions may appear. For example, if an automatic loading function appears in the loaded file, the problem of duplicate function names will arise!

We don’t use the __autoload function at all, but register the user’s own autoloading function to avoid the above situation!

#注册其他的自动加载函数 解决自动加载函数重名问题function f1($class_name){
    if(!file_exists("./".$class_name.".class.php")){
        include "./".$class_name.".class.php";
    }else{
        die("系统错误,没有找到相关的类!");
    }
}
spl_autoload_register('f1');
Copy after login

The following points need to be noted:

1. Registration must occur before a certain class is required!

2, You can register multiple automatic loading functions. Before the required class file is loaded successfully, it will be executed in the order of registration until it is found!

3. Once other autoloading functions are registered, the system's default __autoload function will become invalid!

At this time, if you want to continue to use the __autoload function, you must re-register it like other ordinary functions!

Register automatic loading method

In object-oriented programming style, we usually encapsulate user-defined automatic loading functions into a class!

Registration of static methods

The syntax is:

spl_autoload_register(array('class name','method name'));

In addition, There is also a relatively simple form of registering static methods:

spl_autoload_register('class name::method name');

Registration of non-static methods

At this time, registration The steps are as follows:

1, First, instantiate an object

2, and then register it using spl_autoload_register(). At this time, the parameter form of the function is as follows:

spl_autoload_register( array(object variable, 'method name'));

#注册自动加载方法class Common {
    public static function autoload($class_name){
        if(!file_exists("./".$class_name.".class.php")){
            include "./".$class_name.".class.php";
        }else{
            die("系统错误,没有找到相关的类!");
        }
    }
}
//spl_autoload_register("Common::autoload");
spl_autoload_register(array("Common","autoload"));
Copy after login

Related recommendations:

implementation code of PHP simple routing and class automatic loading function

Detailed explanation of automatic loading of PHP files autoloading

In-depth summary of PHP automatic loading

The above is the detailed content of PHP implements automatic loading of related functions. For more information, please follow other related articles on the PHP Chinese website!

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 Article Tags

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 Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

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 Installation and Upgrade guide for Ubuntu and Debian

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

CakePHP File upload

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

CakePHP Routing

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

Discuss CakePHP

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

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles