0
class Google_Model_SomeThing { ... }
$className = 'google_MODEL_something';

I can create instance of class base on $className. But what if I would like to get proper case class name from $className without creating instance?

I expect something like this:

echo func('google_MODEL_something'); // Google_Model_SomeThing

or

$className = 'google_MODEL_something';
echo $className::class; // Google_Model_SomeThing
l00k
  • 1,525
  • 1
  • 19
  • 29
  • 1
    Here http://stackoverflow.com/questions/5260168/capital-letters-in-class-name-php. PHP is case Insensitive in class naming. – Rahul Kate Feb 05 '16 at 10:56
  • Like I point in my question: I can create instance - PHP is case insensitive when using class name. But how to get proper case class name? – l00k Feb 05 '16 at 10:58
  • That depends on where do you want to get it from ? – Rahul Kate Feb 05 '16 at 11:01

3 Answers3

2

Reflection is most likely a better option, but here is an alternative:

You could use get_declared_classes after calling class_exists($wrongCaseName) and find the class name in the array of declared classes.

Example:

$wrongCaseName = 'Some\classy\THIng';
class_exists($wrongCaseName); //so it gets autoloaded if not already done
$classes = get_declared_classes();
$map = array_combine(array_map('strtolower',$classes),$classes);
$proper = $map[strtolower($wrongCaseName)];

Performance

The reflection-based method, noted by l00k, is significantly faster, by about 3 times. I ran this version vs the reflection version on 500 different classes with randomly generated names & no code within them. The reflection-method took about 0.015 seconds to get the proper case for 500 classes, and my get_declared_classes method took about 0.050 seconds for 500 classes.
More details on my personal website. Tested on PHP 7.2.19 on my localhost server on my laptop.

Reed
  • 14,703
  • 8
  • 66
  • 110
  • 1
    Interesting idea, but I not sure how about performence incluence. – l00k Jun 25 '19 at 20:52
  • I might do some testing to see the performance difference between this and the reflection. I'm curious. Probably depends some upon the number of declared classes. Not sure how performant reflection is either. I'll update with my results – Reed Jun 26 '19 at 04:43
  • 1
    @l00k, reflection is WAYYY faster. By about 3 times. Answer updated. They're both super fast, but if you're trying to reduce your carbon footprint, reflection looks like the way to go. – Reed Jun 26 '19 at 16:01
  • 1
    Good job! Placeholder for comment. – l00k Jun 26 '19 at 19:33
1

I found code below working, but is it the simplest solution?

$className = 'google_MODEL_something';
$reflection = new ReflectionClass($className);
echo $reflection->getName(); // Google_Model_SomeThing
l00k
  • 1,525
  • 1
  • 19
  • 29
0

Well I would suggest to create an instance and then get the className using get_class().

$bar = new $classname();
$caseClassName = get_class($bar);
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
  • Yea that is the simplest solution. But I cant use it, becouse of performance ascpect. Constructing may take too much time. – l00k Feb 05 '16 at 11:07
  • The classes for which I need the proper case don't necessarily have empty constructors. Some do. Some don't. This is the first thing I tried before searching online! – Reed Jun 26 '19 at 16:03