PHP:get_drawn_class()与get_class($ this) [英] PHP: get_called_class() vs get_class($this)
问题描述
在PHP中 get_called_class()
有什么区别和 get_class($this)
(在实例内部使用)?
In PHP what is the difference between get_called_class()
and get_class($this)
when used inside an instance?
示例:
class A {
function dump() {
echo get_called_class();
echo get_class($this);
}
}
class B extends A {}
$A = new A();
$B = new B();
$A->dump(); // output is 'AA'
$B->dump(); // output is 'BB'
在这种情况下有什么区别吗?
Is there any difference in this case?
我什么时候应该使用一个或另一个get_called_class()
或get_class($this)
?
When should I be using one or the other get_called_class()
or get_class($this)
?
推荐答案
在这种情况下没有区别,因为$this
始终指向使用get_class()
解析类名的正确实例.
In this case there's no difference, because $this
always points to the correct instance from which the class name is resolved using get_class()
.
函数get_called_class()
用于静态方法.重写静态方法时,此函数将返回提供正在调用的当前方法的上下文的类名称.
The function get_called_class()
is intended for static methods. When static methods are overridden, this function will return the class name that provides the context for the current method that's being called.
这篇关于PHP:get_drawn_class()与get_class($ this)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!