如何判断PHP中是否静态调用了函数? [英] How can I tell if a function is being called statically in PHP?
本文介绍了如何判断PHP中是否静态调用了函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何分辨我是静态对象还是物体?
Possible Duplicate:
How to tell whether I’m static or an object?
假设我有一个FooClass
和一个bar()
方法.在bar()
方法内部,是否有任何方法可以判断它是否被静态调用,因此我可以对这两种情况进行不同的处理?
Let's say I have a FooClass
with a bar()
method. Inside of the bar()
method, is there any way to tell if it's being called statically or not, so I can treat these two cases differently?
FooClass::bar();
$baz = new FooClass();
$baz->bar();
推荐答案
class FooClass {
function bar() {
if ( isset( $this ) && get_class($this) == __CLASS__ ) {
echo "not static";
}
else {
echo "static";
}
}
}
FooClass::bar();
$baz = new FooClass();
$baz->bar();
这篇关于如何判断PHP中是否静态调用了函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文