Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use autoLoad in PHP

Shulou Source: shulou.com Published: 2022-06-03 18:59:58 09月13日 Update

This article mainly explains the "specific use of autoLoad in PHP", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the specific use of autoLoad in PHP" bar!

How to use _ _ autoload 1:

The most commonly used method is to find out the class file according to the class name, and then require_one

The copy code is as follows:

Function _ _ autoload ($class_name) {

$path = str_replace ('_','/', $class_name)

Require_once $path. '.php'

}

/ / the Http/File/Interface.php file will be loaded automatically here

$a = new Http_File_Interface ()

The advantage of this method is that it is easy to use. Of course, there is also a disadvantage, the disadvantage is that the class name and file path are forced to do a convention, when modifying the file structure, it is necessary to modify the class name.

Usage of _ _ autoload 2 (direct mapping method)

The copy code is as follows:

$map = array (

'Http_File_Interface' = >' CGRUGUGUP PHPGRAPHY HTTP Universe FILEUniver Interface.php'

);

Function _ _ autoload ($class_name) {

If (isset ($map [$class_name])) {

Require_once $map [$class_name]

}

}

/ / the C:/PHP/HTTP/FILE/Interface.php file will be loaded automatically here

$a = new Http_File_Interface ()

The advantage of this method is that the class name and file path are only maintained by a mapping, so when the file structure changes, there is no need to change the class name, just modify the corresponding items in the mapping.

The disadvantage of this approach over the previous approach is that the mapping is very troublesome to maintain when there are too many files, so you may consider using json or a single file for maintenance. You may want to use a framework to maintain or establish such a mapping.

Spl_autoload

The biggest drawback of _ _ autoload is that it cannot have multiple autoload methods

Well, consider the following scenario where your project references a project of someone else's, your project has a _ _ autoload in your project, and someone else's project has a _ _ autoload, so the two _ _ autoload conflict. The solution is to modify _ _ autoload to become one, which is undoubtedly very tedious.

So we urgently need to use an autoload call stack so that spl's autoload series of functions appear. You can use spl_autoload_register to register multiple custom autoload functions

If your PHP version is greater than 5.1, you can use spl_autoload

Let's take a look at several functions of spl:

Spl_autoload, the default implementation of _ autoload (), looks for $class_name (.php / .inc) in include_path.

Spl_autoload implements automatic loading:

The copy code is as follows:

/ * http.php*/

Tags: Methods code files functions methods of use that is class names times projects shortcomings paths learning examples content multiple benefits structures facets maximum necessary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi MySQL Shulou Information MariaDB