How can I tell if a function is being called stati

2020-02-07 05:46发布

Possible Duplicate:
How to tell whether I’m static or an object?

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();

标签: php oop
1条回答
淡お忘
2楼-- · 2020-02-07 06:12
class FooClass {

    function bar() {
        if ( isset( $this ) && get_class($this) == __CLASS__ ) {
            echo "not static";
        }
        else {
            echo "static";
        }
    }

}

FooClass::bar();
$baz = new FooClass();
$baz->bar();
查看更多
登录 后发表回答