PHP 类的命名空间 和自动载入
2024-09-23 10:21:04
PHP 类的自动载入有两种方法,__autoload() 和 spl_autoload_register() ,就是在PHP代码中new一个类的时候,会自动触发,将类的类名包括命名空间作为参数传进入方法里,在方法里可根据命名空间和类名准确找到类文件,从而require或者inlcude进来。菜鸟一枚,作为备忘
<?php
function auto($class){
//$class = A\B\E;
/** 命名空间的自动载入 **/
$class_path = explode("\\",$class);
$file = __DIR__ . '/' ;
foreach($class_path as $c){
$file .= $c . '/';
}
$file = rtrim($file,"/");
$file .= '.php';
var_dump($file);exit;
}
spl_autoload_register('auto');
use A\B\E;
$e = new E();
echo 'hi';
/*******输出*******/
string(32) "/www/test_php_autoload/A/B/E.php"
最新文章
- SSDB 主从配置
- AndroidUI优化工具——HierarchyViewer
- CentOS 6.5下源码安装MySQL 5.6
- [iOS UI进阶 - 2.0] 彩票Demo v1.0
- Linux shell 脚本攻略之批量重命名
- 切换加上延迟加载js代码
- 如何使用event 10049分析定位library cache lock and library cache pin
- Aforge.net 一个专门为开发者和研究者基于C#框架设计
- vi编辑器常见命令的使用
- 【小程序】微信小程序实现各种特效实例
- Redis环境搭建
- response 输出中文数据 文件下载
- Spring的声明式事务管理<;tx:advice/>;
- eclipse 如何安装freemaker ftl 插件
- 检测cpu、主板、内存
- 前端框架之Vue(2)-模板语法
- Git-管理和撤销修改
- Git创建分支/GIT提交分支
- Suse系统中不使用SFTP的话,还可以使用lrzsz。
- FineUI 选中多行获取行ID
热门文章
- Android+openCV 动态人脸检测
- AtCoder Grand Contest 027 (AGC017) D - Modulo Matrix 构造
- There is no getter for property named &#39;XXX&#39; in &#39;class java.lang.String&#39;解决方法
- sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding=&#39;utf8&#39;) #改变标准输出的默认编码
- Wxpython入门
- url传参过程中文字需编码、解码使用
- Centos6.5部署vsftpd+mysql认证
- 【自动化测试】robot framwork的一点小发现
- maven deploy Return code is: 400, ReasonPhrase: Bad Request.
- aspose 生成word 简单的文档操作