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 to realize automatic loading Mechanism in PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

PHP how to use AutoLoad to achieve automatic loading mechanism, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Usage of _ _ autoload 1

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

View sourceprint?

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)

View sourceprint?

$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 (in include_path. Php/.inc)

Spl_autoload implements automatic loading:

View sourceprint?

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

Internet Technology

Wechat

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

12
Report