[英]How do I get class name in PHP?
public class MyClass {
}
In Java, we can get class name with String className = MyClass.class.getSimpleName();
在 Java 中,我们可以得到 class 名称
String className = MyClass.class.getSimpleName();
How to do this in PHP?如何在 PHP 中执行此操作? I already know
get_class()
, but it works only for objects.我已经知道
get_class()
,但它仅适用于对象。 Currently I work in Active Record.目前我在 Active Record 工作。 I need statement like
MyClass::className
.我需要像
MyClass::className
这样的声明。
Since PHP 5.5 you can use class name resolution via ClassName::class .从 PHP 5.5 开始,您可以通过ClassName::class使用类名解析。
See new features of PHP5.5 .查看PHP5.5 的新特性。
<?php
namespace Name\Space;
class ClassName {}
echo ClassName::class;
?>
If you want to use this feature in your class method use static::class :如果要在类方法中使用此功能,请使用static::class :
<?php
namespace Name\Space;
class ClassName {
/**
* @return string
*/
public function getNameOfClass()
{
return static::class;
}
}
$obj = new ClassName();
echo $obj->getNameOfClass();
?>
For older versions of PHP , you can use get_class() .对于旧版本的 PHP ,您可以使用get_class() 。
You can use __CLASS__
within a class to get the name.您可以在类中使用
__CLASS__
来获取名称。
http://php.net/manual/en/language.constants.predefined.php http://php.net/manual/en/language.constants.predefined.php
It sounds like you answered your own question.听起来你回答了你自己的问题。
get_class
will get you the class name. get_class
将为您提供班级名称。 It is procedural and maybe that is what is causing the confusion.这是程序性的,也许这就是造成混乱的原因。 Take a look at the php documentation for
get_class
看一看
get_class
的php文档
Here is their example:这是他们的例子:
<?php
class foo
{
function name()
{
echo "My name is " , get_class($this) , "\n";
}
}
// create an object
$bar = new foo();
// external call
echo "Its name is " , get_class($bar) , "\n"; // It's name is foo
// internal call
$bar->name(); // My name is foo
To make it more like your example you could do something like:为了使它更像您的示例,您可以执行以下操作:
<?php
class MyClass
{
public static function getClass()
{
return get_class();
}
}
Now you can do:现在你可以这样做:
$className = MyClass::getClass();
This is somewhat limited, however, because if my class is extended it will still return 'MyClass'.然而,这在某种程度上是有限的,因为如果我的课程被扩展,它仍然会返回“MyClass”。 We can use
get_called_class
instead, which relies onLate Static Binding , a relatively new feature, and requires PHP >= 5.3.我们可以使用
get_called_class
代替,它依赖于Late Static Binding ,这是一个相对较新的功能,并且需要 PHP >= 5.3。
<?php
class MyClass
{
public static function getClass()
{
return get_called_class();
}
public static function getDefiningClass()
{
return get_class();
}
}
class MyExtendedClass extends MyClass {}
$className = MyClass::getClass(); // 'MyClass'
$className = MyExtendedClass::getClass(); // 'MyExtendedClass'
$className = MyExtendedClass::getDefiningClass(); // 'MyClass'
Now, I have answer for my problem.现在,我有我的问题的答案。 Thanks to Brad for the link, I find the answer here .
感谢Brad提供的链接,我在这里找到了答案。 And thanks to J.Money for the idea.
并感谢J.Money的想法。 My solution:
我的解决方案:
<?php
class Model
{
public static function getClassName() {
return get_called_class();
}
}
class Product extends Model {}
class User extends Model {}
echo Product::getClassName(); // "Product"
echo User::getClassName(); // "User"
To get class name you can use ReflectionClass要获取类名,您可以使用 ReflectionClass
class MyClass {
public function myNameIs(){
return (new \ReflectionClass($this))->getShortName();
}
}
It looks like ReflectionClass
is a pretty productive option.看起来
ReflectionClass
是一个非常高效的选择。
class MyClass {
public function test() {
// 'MyClass'
return (new \ReflectionClass($this))->getShortName();
}
}
Benchmark:基准:
Method Name Iterations Average Time Ops/second
-------------- ------------ -------------- -------------
testExplode : [10,000 ] [0.0000020221710] [494,518.01547]
testSubstring : [10,000 ] [0.0000017177343] [582,162.19968]
testReflection: [10,000 ] [0.0000015984058] [625,623.34059]
I think it's important to mention little difference between 'self' and 'static' in PHP as 'best answer' uses 'static' which can give confusing result to some people.我认为重要的是要提到 PHP 中“自我”和“静态”之间的细微差别,因为“最佳答案”使用了“静态”,这可能会给某些人带来令人困惑的结果。
<?php
class X {
function getStatic() {
// gets THIS class of instance of object
// that extends class in which is definied function
return static::class;
}
function getSelf() {
// gets THIS class of class in which function is declared
return self::class;
}
}
class Y extends X {
}
class Z extends Y {
}
$x = new X();
$y = new Y();
$z = new Z();
echo 'X:' . $x->getStatic() . ', ' . $x->getSelf() .
', Y: ' . $y->getStatic() . ', ' . $y->getSelf() .
', Z: ' . $z->getStatic() . ', ' . $z->getSelf();
Results:结果:
X: X, X
Y: Y, X
Z: Z, X
即使使用命名空间,这也将返回纯类名:
echo substr(strrchr(__CLASS__, "\\"), 1);
end(preg_split("#(\\\\\\\\|\\\\/)#", Class_Name::class))
Class_Name::class
: return the class with the namespace. Class_Name::class
:返回具有命名空间的类。 So after you only need to create an array, then get the last value of the array.所以在你只需要创建一个数组之后,然后获取数组的最后一个值。
<?php
namespace CMS;
class Model {
const _class = __CLASS__;
}
echo Model::_class; // will return 'CMS\Model'
for older than PHP 5.5早于PHP 5.5
From PHP 8.0, you can use ::class
even on objects:从 PHP 8.0 开始,您甚至可以在对象上使用
::class
:
$object = new \SplPriorityQueue();
assert($object::class === \SplPriorityQueue::class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.