PHP类名称为字符串

ast*_*ius 4 php class

是否有内置的静态方法或属性来引用PHP类,以便它在上下文中表示为字符串?例如:

而不是这个:

$obj->tempFn('MyClass') //MyClass being the name of the class
Run Code Online (Sandbox Code Playgroud)

我想做这个:

$obj->tempFn(MyClass) //Directly references the class name, instead of a string representation
Run Code Online (Sandbox Code Playgroud)

nic*_*ass 7

不.但您可以在类中定义一个包含类名的常量,例如:

class foo{
  const  
    NAME = 'foo';
}
Run Code Online (Sandbox Code Playgroud)

并访问它foo::NAME.

PHP 5.5中,您将能够使用:

foo::class
Run Code Online (Sandbox Code Playgroud)