Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active September 29, 2025 03:50
Show Gist options
  • Select an option

  • Save Shelob9/c4921126666b0950e046 to your computer and use it in GitHub Desktop.

Select an option

Save Shelob9/c4921126666b0950e046 to your computer and use it in GitHub Desktop.
Get name of class, without the namespace. get_called_class() used instead of __CLASS__ to ensure that if class is extended, that class' name is used.
<?php
$class = get_called_class();
$class = explode( '\\', $class );
end( $class );
$last = key( $class );
$class = $class[ $last ];
/**
* Get the class' name
*
* @return array|string
*/
public function get_class_name( $without_namespace = true ) {
$class = get_called_class();
if ( $without_namespace ) {
$class = explode( '\\', $class );
end( $class );
$last = key( $class );
$class = $class[ $last ];
}
return $class;
}
@g4li
Copy link

g4li commented Sep 29, 2025

Or if you want a one liner: substr(get_called_class(), strrpos(get_called_class(), '\\') + 1);

shorter is better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment