Skip to content

Instantly share code, notes, and snippets.

@jasondmoss
Last active February 18, 2021 00:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jasondmoss/6200807 to your computer and use it in GitHub Desktop.
Save jasondmoss/6200807 to your computer and use it in GitHub Desktop.
Strip the namespace from the class to get the actual class name
<?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;
}
/* <> */
@Petah
Copy link

Petah commented Oct 29, 2015

No regex required: substr($className, strrpos($className, '\\') + 1)

@a-r-m-i-n
Copy link

Actually on Windows it also works with basename($className);. But just because the DIRECTORY_SEPARATOR is a backslash on Windows ^^

@AliShareei
Copy link

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