How to tell whether I'm static or an object?

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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Admittedly not offhand…but Sean Coates has a cool and fairly simple approach to finding this out:

$isStatic = !(isset($this) && get_class($this) == __CLASS__);

Solution 2

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply