Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 CRMPiccoBundle\Services\RFC\Webhook\SiteCancelled

namespace CRMPiccoBundle\Services\RFC\Webhook;

class SiteCancelled extends Base implements Interface
{
    public function process() {
       // echo get_class()
    }
}
like image 562
crmpicco Avatar asked Oct 27 '25 10:10

crmpicco


2 Answers

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.

like image 187
yivi Avatar answered Oct 28 '25 22:10

yivi


Use the class_basename() function to get your class name without the namespace

<?php

echo class_basename(__CLASS__);
like image 43
Mostafa Soufi Avatar answered Oct 28 '25 22:10

Mostafa Soufi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!