Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use autoLoad in PHP

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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*/

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report