Last Updated: February 25, 2016
·
450
· 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);
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside