PHP 获取不带命名空间的类名

2020-07-30 13:31:28

方法很多,列出几个,以供参考。

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 FooBar does not exist

更多PHP相关技术文章,请访问PHP教程栏目进行学习!

相关文章 大家在看