Home  >  Article  >  php教程  >  PHP 类自动加载机制

PHP 类自动加载机制

WBOY
WBOYOriginal
2016-06-16 08:41:11932browse

PHP 类自动加载机制 spl_autoload_register()

版本

PHP5.1.2 之前使用 __autoload() 。
PHP5.1.2 之后使用 spl_autoload_register() 。
本文以 spl_autoload_register() 进行演示 。

包含目录

application/controllers
application/models
application/libs

代码实现//包含目录<br> $include_dir = [<br>   'application/controllers',<br>   'application/models',<br>   'application/libs'<br> ]<br> //设置包含目录<br> set_include_path(get_include_path() . PATH_SEPARATOR .implode(PATH_SEPARATOR, $include_dir));<br> /**<br>  * 自动加载类库<br>  * @param string $class 类名<br>  */<br> function auto_load_class($class = '')<br> {<br>     //在这可以进行扩展,默认是将类名转成小写。<br>     //可扩展方向:文件夹_类名 <br>     $path = strtolower($class) . '.class.php';<br>     include_once($path);<br> }<br> spl_autoload_register('auto_load_class'); //spl注册自动加载<br> $obj = new 类名(); //实例化<br> $obj->方法名();<br> <br> //当指定了多个目录且有相同名称的文件时,以排位居前目录下的为准。更多【干货分享】,第一时间获取,请关注 PHP工程师 订阅号。

PHP 类自动加载机制

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
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