PHP类的自动加载
2015-09-24 15:37
453 查看
<?php namespace Manager; /** * Class autoLoad * @param $classMap * @package Manager */ class autoLoad{ /** * save the file path of class * @var array */ public static $classMap = array(); /** * include class * @param $className * if the param is a string , it must be a namespace path * @author pig 2015-09-22 */ public static function loadClass($className){ if(isset(self::$classMap[$className])){ include self::$classMap[$className]; }else{ $className and $filePath = str_replace('\\','/',$className).'.php'; if(!file_exists($filePath)){ exit("file:$filePath is not exist"); } self::$classMap[$className] = $filePath; include $filePath; } $result = class_exists($className,false) || interface_exists($className,false); if(!$result){ exit("class:$className is not exist"); } } /** * auto load register function * @author pig 2015-09-22 */ public static function run(){ spl_autoload_register(__NAMESPACE__ .'\autoLoad::loadClass'); } }
如何使用?
<?php use Manager\BlogManager; include 'Manager/autoLoad.php'; autoLoad::run(); BlogManager::factory()->encode(); //这时就可以自动把BlogManager类自动包含进来
相关文章推荐
- 又十个超级有用的PHP代码片段
- TP-登录
- phpcms v9模块操作列表
- PHP易混淆知识整理笔记
- php $_SERVER当前完整url的写法
- PHP直接修改表内容DataGrid功能实现代码
- php操作mysql
- PHP implode()函数
- 解决Yii2中刷新网页时验证码不刷新的问题
- php多数组关联
- PHP生成条形码大揭秘
- PHP学习笔记(五):函数
- php读取和保存base64编码的图片内容
- phpcms v9 数据库操作函数
- php商品对比功能代码分享
- 详解sftp实现对远程服务器的文件操作
- php文件
- 高性能SOCKET扩展SWOOLE和PHP框架配置
- 用php实现百度网盘图片直链的代码分享
- 安装php phalcon扩展