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

Introduction to the usage of automatic Loader for PHP Class Files

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

Share

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

This article mainly introduces the "introduction to the usage of the automatic loader for PHP files". In the daily operation, I believe that many people have doubts about the usage of the automatic loader for PHP files. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about the usage of the automatic loader for PHP files. Next, please follow the editor to study!

1. Automatic loader for class files

1. In our project directory, all the server code introduces a common file, which is init.php.

two。 In this file, we load two configuration files. Let's look at global.inc.php first.

There is a class file autoloader in this file.

3. Spl_autoload_register () returns true as a substitute for the _ _ autoload () function.

4. Detailed explanation of process

We can print out $class_name.

Since our project is stored on linux, there is no backslash on linux, so we need to change the slash to forward slash.

Then splice the path and determine whether the file exists or not

Then call it outside the class.

two。 Explain the file class loader

If a class needs to be used in multiple scripts, the definition code of a class can be encapsulated separately into a file, which is also called a class file, and the entire file can be loaded when needed!

When PHP executes, if you find that you need a class (as long as it is any operation related to this class), but the current script does not have the definition code of this class, nor does it load the class file manually, then PHP will automatically call a function called _ _ autoload ($class_name). When it is called, it will pass a parameter to the function, which is the class name of the class you currently need!

3. Inspection

Let's write a random model instance in the list file.

Then print and you can see class_name.

But we don't have this file, so we will report file not exists.

3. The source code / / custom class automatically loads the path define ('CUSTOM_CLASS_PATH', ROOT_PATH. / class'); define ('DIRECTORY_SEPARATOR', "/"); / * Class SYSCore * autoloader * / class SYSCore {public static function registerAutoload ($class =' SYSCore') {spl_autoload_register ($class, 'autoload');} public static function unregisterAutoload ($class) {spl_autoload_unregister ($class,' autoload')) } public static function my_callback ($match) {return DIRECTORY_SEPARATOR. $match [0] } public static function autoload ($class_name) {if (strpos ($class_name, 'common') = 0 | | strpos ($class_name,' model') = 0 | | strpos ($class_name, 'Mysql') = 0 | | strpos ($class_name,' Curl') = 0 | strpos ($class_name) 'controller') = 0) {/ / the custom class domain name space within the system} else {return true } $class_name = str_replace ('\','/', $class_name); $class_path = CUSTOM_CLASS_PATH. DIRECTORY_SEPARATOR. $class_name.'.php'; $class_path = str_replace ('/ /','/', $class_path); # determine whether the file exists if (file_exists ($class_path)) {# introduce return include_once ($class_path) if so } else {# report warning message echo "file not exists class_path=$class_path\ n";} return false;}} SYSCore::registerAutoload (); at this point, the study on "introduction to the usage of autoloader for PHP files" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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