Does anybody know offhand the way to tell whether I'm being called statically (Classname::function) or inside an object ($classInstance->function) inside a PHP method?
            Asked
            
        
        
            Active
            
        
            Viewed 1,892 times
        
    2
            
            
        - 
                    1That's what the static keyword is there for. If you're mixing the two up you should be getting a warning. – Azeem.Butt Oct 30 '09 at 02:50
- 
                    A static function should not be called non-statically, and a non-static method should not be called statically. Yet another thing in PHP that should generate a fatal error, but does not. – Annika Backstrom Oct 30 '09 at 03:25
- 
                    In the case at hand, I had to fix a bug quickly to get to bed :) But you're right, of course. – Pekka Oct 30 '09 at 04:01
- 
                    possible duplicate of [How do I check in PHP that I'm in a static context (or not)?](http://stackoverflow.com/questions/1858538/how-do-i-check-in-php-that-im-in-a-static-context-or-not) – Jocelyn Oct 28 '12 at 23:52
2 Answers
10
            Admittedly not offhand...but Sean Coates has a cool and fairly simple approach to finding this out:
$isStatic = !(isset($this) && get_class($this) == __CLASS__);
 
    
    
        Ben Regenspan
        
- 10,058
- 2
- 33
- 44
1
            
            
        Check if $this is set and equal to the class. It will be equal for an instance call and non-equal (or null) for a static call.
 
    
    
        Amber
        
- 507,862
- 82
- 626
- 550
- 
                    This is not actually correct. See my linked blog post in the accepted answer. `$this` can be set if a method is called statically, and it might not even belong to the containing class. – scoates Dec 10 '12 at 17:21
