__autoload() is a function agreed in the PHP execution environment rather than a method of a certain class. If a class is not loaded into the current file before use, the __autoload() function will be automatically called to load the class. Usually these The loading rules of classes are agreed upon. For example, these classes are included in files named after the class names. This method can realize on-demand loading of classes and avoid loading unnecessary classes before script execution, thereby reducing resource usage and submission performance.
Note: Errors within __autoload() cannot be caught by try-catch.
The code is as follows | Copy code | ||||
require_once(PATH.'/calsses/'.$class_name.'.php'); } $obj1 = new mycalss1();
|
代码如下 | 复制代码 |
spl_autoload_register(array('class_name'|$obj,'method_name')); 例如: spl_autoload_register(array($this,'autoloadClass')); |
The spl code base is automatically enabled by default after PHP5.0
spl_autoload_register([callback]); //Instead of writing the specific loading code in __autoload(), you can use this function to register the callback function.
If you use a class method as a callback function, you need to pass in an array:
The code is as follows | Copy code |
spl_autoload_register(array('class_name'|$obj,'method_name')); For example: spl_autoload_register(array($this,'autoloadClass')); |
spl_autoload_register(array('YiiBase','autoload'));//Implementation of the autoloading class of the YII framework, the YiiBase class implements an autoload method. spl_autoload_register() can register multiple load functions, and all registered load functions will be tried one by one before successfully loading the class file. This is useful when different classes use different logic to import class files.
spl_autoload_unregister(); //Cancel a registered load function, the parameters are the same as spl_autoload_register().
代码如下 | 复制代码 |
spl_autoload_extentions(".class.php"); spl_autoload_register(); //使用spl_autoload() 来尝试自动加载类文件 //这样 spl_autoload('myclassName'); 会尝试加载 文件 "myclassName.class.php" . |
The code is as follows | Copy code |
spl_autoload_extensions(".class.php"); spl_autoload_register(); //Use spl_autoload() to try to automatically load class files //In this way spl_autoload('myclassName'); will try to load the file "myclassName.class.php" . |
Example
1. Put the classes that need to be registered in an array
The code is as follows
|
Copy code
|
||||||||||||||||
} public static function getClasses($pre_path = '/') { $classes = array( 'DBConfig' => $pre_path.'DBConfig/DBConfig.php', 'User' => $pre_path.'Model/User.php', 'Dao' => $pre_path.'Dao/Dao.php', 'UserDao' => $pre_path.'Dao/UserDao.php', 'UserMapper' => $pre_path.'Mapping/UserMapper.php', ); return $classes; } } ?>
source:php.cn
Previous article:PHP infinite hierarchical data implementation program code_PHP tutorial
Next article:Things to note when using empty() function in php_PHP Tutorial
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|