This article mainly introduces the php auto-loader implementation method, which involves php's skills on file reading, judgment, and string operations. it has some reference value, for more information about how to implement php auto-loaders, see the example in this article. Share it with you for your reference. The details are as follows:
Here, autoload is compatible with the following formats:
Cache_File_Json
Class_xxx.php
Xxx. class. php
Xxx. php
The php code is as follows:
Function _ autoload ($ className) {$ dirs = explode ('_', $ className); $ fileName = array_pop ($ dirs); // print_r ($ dirs ); $ filePath = $ fileName; if (is_array ($ dirs) & (count ($ dirs)> 0) {// echo '\ n --- \ n '; print_r ($ dirs); $ dirPath = ''; foreach ($ dirs as $ dir) {if ($ dir) {$ dirPath. = strtolower ($ dir ). DIRECTORY_SEPARATOR; }}$ filePath = $ dirPath. $ fileName. '. php ';} else {if (file_exists ('class _'. $ fileName. '. php ') {$ filePath = 'class _'. $ fileName. '. php ';} else {if (file_exists ($ fileName. '. class. php ') {$ filePath = $ fileName. '. class. php ';} else {$ filePath = $ fileName. '. php ';}}// var_dump ($ filePath); require $ filePath ;}
I hope this article will help you with php programming.