How do I get class name in PHP?

后端 未结 10 1975
名媛妹妹
名媛妹妹 2020-11-29 04:37
public class MyClass {

}

In Java, we can get class name with String className = MyClass.class.getSimpleName();

How to do this

10条回答
  •  [愿得一人]
    2020-11-29 05:03

    It looks like ReflectionClass is a pretty productive option.

    class MyClass {
        public function test() {
            // 'MyClass'
            return (new \ReflectionClass($this))->getShortName();
        }
    }
    

    Benchmark:

    Method Name      Iterations    Average Time      Ops/second
    --------------  ------------  --------------    -------------
    testExplode   : [10,000    ] [0.0000020221710] [494,518.01547]
    testSubstring : [10,000    ] [0.0000017177343] [582,162.19968]
    testReflection: [10,000    ] [0.0000015984058] [625,623.34059]
    
    

提交回复
热议问题