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 CRMPiccoBundle\Services\RFC\Webhook\SiteCancelled
namespace CRMPiccoBundle\Services\RFC\Webhook;
class SiteCancelled extends Base implements Interface
{
public function process() {
// echo get_class()
}
}
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.
Use the class_basename() function to get your class name without the namespace
<?php
echo class_basename(__CLASS__);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With