problem when getting the class name

0

In my code I have a method that uses get_class to get the name of the class. For example, if you had the User class, you would store that class name in a variable.

I recently started using the namespace with psr-4, but now that function get_class returns the namespace of the class. In other words, User returns App \ Models \ User

How can I make it work as before without removing the namespace?

This is the method, only the part of the get_class ($ this) is the one that works like I do not want:

public function parseClassNameToTable(){

    $className = preg_replace('/(?<=\w)(\p{Lu})/u', '_$1', get_class($this));
    $className = mb_strtolower($className);

    return $className . 's';
}
    
asked by kmilo93sd 21.03.2018 в 21:20
source

1 answer

1

Since PHP 5.4 The CLASS constant returns the FQNS (Fully Qualified Namespace) of the class. You could do:

    $FQNS_array = explode('\', __CLASS__);
    $className  = array_pop($FQNS_array);
    
answered by 22.03.2018 в 13:09