PHP function to obtain model_name from Namespace\To\ModelName Would there be a more clear/efficient way to write

public function getModelName()
{
    return substr(strtolower(substr(strrchr(preg_replace('/(?&lt;!\ )[A-Z]/', '_$0', __CLASS__), "\\"), 1)), 1);
}


for example:

__CLASS__: Namespace\To\SettingOption

Output: setting_option
You tagged laravel, so this answer assumes you are using Laravel.

Laravel has two built in helper functions that will help you do this. class_basename() will return the name of the class with the namespace stripped off, and snake_case() will convert a string to snake case.

Combine them, and you get:

public function getModelName()
{
    return snake_case(class_basename(__CLASS__));
}