Quick Fix: You can do this with reflection.Build a ReflectionClass
instance, and then call the getShortName
method of that instance:
$reflect = new ReflectionClass($object);
if ($reflect->getShortName() === 'Name') {
// do this
}
The Problem:
Given an object within a PHP namespace environment, you want to check its class without specifying the full namespaced class name. For instance, if you have an object from the library/Entity/Contract/Name namespace, using get_class($object) == ‘Name’ won’t work because it returns the fully qualified class name. The namespace magic keyword provides the current namespace, but it’s not helpful if the tested object has a different namespace. While specifying the full class name with namespaces is an option, it can limit code flexibility. Is there an efficient way to determine an object’s class without the full namespace?
The Solutions:
Solution 1: Using ReflectionClass::getShortName() method
You can use the ReflectionClass::getShortName
method to get the unqualified (short) class name of an object. This method returns the name of the class without its namespace.
Here’s an example of how to use this method:
$object = new \library\Entity\Contract\Name();
$reflect = new \ReflectionClass($object);
$shortName = $reflect->getShortName(); // "Name"
However, keep in mind that using reflection can have performance implications, so it’s generally not recommended to use it in performance-critical code.
Solution 2: Reflection
The best and most efficient solution is to use the ReflectionClass built-in class. It provides a way to inspect the class of an object, including its short name:
(new \ReflectionClass($obj))->getShortName();
Solution 3: Substring
This solution uses the substr() and strrpos() functions to extract the unqualified class name. The strrpos() function finds the last occurrence of the backslash (\) character in the full class name, and the substr() function is then used to extract the characters after that position. This approach is relatively efficient and provides the desired output.
Php Code
$classNameWithNamespace = get_class($this);
return substr($classNameWithNamespace, strrpos($classNameWithNamespace, '\\') + 1);
\n
Solution 4: Laravel class_basename()
\n
Here is a more easier way of doing this if you are using Laravel PHP framework :
<?php
// usage anywhere
// returns HelloWorld
$name = class_basename('Path\To\YourClass\HelloWorld');
// usage inside a class
// returns HelloWorld
$name = class_basename(__CLASS__);
/**
* Get the class "basename" of the given object / class.
*
* @param string|object $class
* @return string
*/
function class_basename($class)
{
$class = is_object($class) ? get_class($class) : $class;
return basename(str_replace('\\', '/', $class));
}.</p>
Solution 5: Using basename() to get unqualified class name
This solution employs the basename() function to extract the unqualified (short) class name from the full namespaced class string. The str_replace() function is first used to convert any namespace separators (\) to forward slashes (/), making the class name compatible with the basename() function. The resulting short class name is then returned.