PHP class auto-loading mechanism
Oct 10, 2016 am 10:08 AMAutomatic loading of php:
Before php5, if we want to use a certain class or class method, it must include or require before it can be used. Every time a class is used, an include needs to be written, which is troublesome
php author To make it simple, it is best to reference a class. If there is no include currently, the system can automatically find the class and automatically introduce it~
So: the __autoload() function came into being.
Usually placed in the application entry class, such as discuz, placed in class_core.php.
Let’s talk about a simple example first:
The first case: the content of file A.php is as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
The second case: Sometimes I hope to customize autoload and want to give a cooler name for loader, then C .php is changed to the following:
1 2 3 4 5 6 7 8 9 10 11 |
|
The third situation: I hope to be more advanced and use a class to manage automatic loading
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
This is currently the best form.
Usually we put spl_autoload_register(*) in the entry script, that is, quoted from the beginning. For example, the method of discuz below.
1 2 3 4 5 6 7 |
|
It’s great to put this paragraph at the front of the entry file~

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
