Skip to content
Advertisement

Get class name minus namespace without using Reflection

Is there a simple way to get the class name without the namespace without using Reflection?

This is my class and when I call get_class() I get CRMPiccoBundleServicesRFCWebhookSiteCancelled

namespace CRMPiccoBundleServicesRFCWebhook;

class SiteCancelled extends Base implements Interface
{
    public function process() {
       // echo get_class()
    }
}

Advertisement

Answer

Or simply exploding the return from class_name and getting the last element:

 $class_parts = explode('\', get_class());
 echo end($class_parts);

Or simply removing the namespace from the output of get_class:

echo str_replace(__NAMESPACE__ . '\', '', get_class());

Either works with or without namespace.

And so on.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement