2016-02-05 44 views
0
class Google_Model_SomeThing { ... } 
$className = 'google_MODEL_something'; 

我可以在$ className上创建类的实例。但是如果我想从$ className中获取正确的案例类名而不创建实例呢?PHP如何获取正确的案例类名称

我希望这样的事情:

echo func('google_MODEL_something'); // Google_Model_SomeThing 

$className = 'google_MODEL_something'; 
echo $className::class; // Google_Model_SomeThing 
+1

这里http://stackoverflow.com/questions/5260168/capital-letters-in-class-name-php。 PHP在类命名中不敏感。 –

+0

就像我在我的问题中指出:我可以创建实例 - 使用类名时PHP不区分大小写。但如何获得正确的案例类名称? – l00k

+0

这取决于你想从哪里得到它? –

回答

0

嗯,我会用get_class().

$bar = new $classname(); 
$caseClassName = get_class($bar); 
+0

是最简单的解决方案。但我不能使用它,因为性能方面的原因。构建可能需要太多时间。 – l00k

0

我发现代码建议创建一个实例,然后得到一个className下面的工作,但它是最简单的解决方案?

$className = 'google_MODEL_something'; 
$reflection = new ReflectionClass($className); 
echo $reflection->getName(); // Google_Model_SomeThing