PHP 获取不带命名空间的类名
16lz
2021-01-22
方法很多,列出几个,以供参考。
Laravel 源码里扒出来的
class_basename
辅助函数basename(str_replace('\\', '/', $class));
substr
实现substr(strrchr($class, "\\"), 1);// orsubstr($class, strrpos($class, '\\') + 1);
explode
实现array_pop(explode('\\', $class));
ReflectionClass
实现(new \ReflectionClass($class))->getShortName();
其中,ReflectionClass
是最快最保险的方案,但此类必须实际存在,不存在则会抛出 ReflectionException
: Class \Foo\Bar does not exist
。
更多PHP相关技术文章,请访问PHP教程栏目进行学习!
更多相关文章
- 性能优越的轻量级日志收集工具,微软、亚马逊都在用!
- 微软Edge浏览器准备内置屏蔽广告功能
- 微软分析Pypi数据: 5月21日Python3战胜Python2
- [简讯] 微软Linux子系统已经支持中文
- 不用***,如何查看国外技术文章
- 《PHP高级知识进阶》系列技术文章整理收藏
- java漏洞成黑客目标微软呼吁用户更新软件