Last Updated: February 25, 2016
·
243
· codeFareith

Get the unqualified Class Name of an Object (highly performant)

For now, this seems to be the most performant way to get the class name - without any prefixed namespace - of an object:

public function getClassName() {
    $fullClassName = get_class($this);
    $lastSeperator = strrpos($fullClassName, '\\');
    $shortClassName = substr($fullClassName, $lastSeperator + 1);
    return $shortClassName;
}

even shorter:

public function getClassName($obj) {
    $fullClassName = get_class($obj);
    return substr($fullClassName, strrpos($fullClassName, '\\') + 1);
}