Home  >  Article  >  Backend Development  >  PHP get class name without namespace

PHP get class name without namespace

步履不停
步履不停Original
2019-06-21 16:01:014674browse

PHP get class name without namespace

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

  • Laravel 源码里扒出来的 class_basename 辅助函数

    basename(str_replace('\\', '/', $class));
  • substr 实现

    substr(strrchr($class, "\\"), 1);
    // or
    substr($class, strrpos($class, '\\') + 1);
  • explode 实现

    array_pop(explode('\\', $class));
  • ReflectionClass 实现

    (new \ReflectionClass($class))->getShortName();

其中,ReflectionClass 是最快最保险的方案,但此类必须实际存在,不存在则会抛出 ReflectionException: Class \Foo\Bar does not exist

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

The above is the detailed content of PHP get class name without namespace. For more information, please follow other related articles on the PHP Chinese website!

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