A lot of people in other comments wanting to get the classname without the namespace. Some weird suggestions of code to do that - not what I would've written! So wanted to add my own way.
<?php
function get_class_name($classname)
{
if ($pos = strrpos($classname, '\\')) return substr($classname, $pos + 1);
return $pos;
}
?>
Also did some quick benchmarking, and strrpos() was the fastest too. Micro-optimisations = macro optimisations!
39.0954 ms - preg_match()
28.6305 ms - explode() + end()
20.3314 ms - strrpos()
(For reference, here's the debug code used. c() is a benchmarking function that runs each closure run 10,000 times.)
<?php
c(
function($class = 'a\b\C') {
if (preg_match('/\\\\([\w]+)$/', $class, $matches)) return $matches[1];
return $class;
},
function($class = 'a\b\C') {
$bits = explode('\\', $class);
return end($bits);
},
function($class = 'a\b\C') {
if ($pos = strrpos($class, '\\')) return substr($class, $pos + 1);
return $pos;
}
);
?>