Last active
February 18, 2021 00:06
-
-
Save jasondmoss/6200807 to your computer and use it in GitHub Desktop.
Strip the namespace from the class to get the actual class name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Strip the namespace from the class to get the actual class name | |
* | |
* @param string $obj Class name with full namespace | |
* | |
* @return string | |
* @access public | |
*/ | |
function stripNamespaceFromClassName($obj) | |
{ | |
$classname = get_class($obj); | |
if (preg_match('@\\\\([\w]+)$@', $classname, $matches)) { | |
$classname = $matches[1]; | |
} | |
return $classname; | |
} | |
/* <> */ |
Actually on Windows it also works with basename($className);
. But just because the DIRECTORY_SEPARATOR is a backslash on Windows ^^
Actually on Windows it also works with
basename($className);
. But just because the DIRECTORY_SEPARATOR is a backslash on Windows ^^
Laravel Helpers use something like this basename(str_replace('\\', DIRECTORY_SEPARATOR, $class));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No regex required:
substr($className, strrpos($className, '\\') + 1)