How do I check in PHP that I'm in a static context (or not)?

后端 未结 11 1307
时光说笑
时光说笑 2020-11-30 08:16

Is there any way I can check if a method is being called statically or on an instantiated object?

11条回答
  •  难免孤独
    2020-11-30 09:06

    This is an old question, but I'll add an alternative answer.

    There are two magic methods

    __call($name, $arguments)
    is triggered when invoking inaccessible methods in an object context.

    __callStatic($name, $arguments)
    is triggered when invoking inaccessible methods in a static context.

    runTest('in object context');
    
    MethodTest::runTest('in static context');  // As of PHP 5.3.0
    

    Outputs

    Calling object method 'runTest' in object context
    Calling static method 'runTest' in static context

提交回复
热议问题