LoginSignup
3
0

More than 5 years have passed since last update.

オブジェクトのクラス名を取得

Last updated at Posted at 2017-06-26

Explode

public function getName() {
    $path = explode('\\', __CLASS__);
    return array_pop($path);
}

ReflectionClass

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

Substring

return substr(strrchr(__CLASS__, "\\"), 1);   

The winner

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
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0