Is there any way I can check if a method is being called statically or on an instantiated object?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
I actually use this line of code on all my scripts,it works well and it prevents errors.
The use of instanceof is not paranoia:
If class A call class B statically function
$this
may exist on the A scope; I know it's pretty messed up but php does it.instanceof
will fix that and will avoid conflicting with classes that may implement your "semi-static" functions.Checking if
$this
is set won't work always.If you call a static method from within an object, then
$this
will be set as the callers context. If you really want the information, I think you'll have to dig it out ofdebug_backtrace
. But why would you need that in the first place? Chances are you could change the structure of your code in a way so that you don't.Edit: beaten to it.
Just return the class as new within the function.