PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Does anyone know of a way to determine whether a classes member function was called statically via the scope resolution operator :: or normally via ->
Is there anyway to determine this inside the function just called???
I hoping that PHP's powerful RTTI model supports such a thing...sure would be neat despite haveing another simple work-aorund...I think this approach would be much more elegant
It can be read from the result of debug_backtrace(), but I wouldn't rely on it, since it's a debugging function and might change when you least expect it ...
/**
* isStaticCall
* checks the call-stack to see if its calling method is being called statically
* @param $aOffset integer how many more steps back into the call stack should it step
* @author feyd
* @return boolean
*/
function isStaticCall($Offset=0) {
$stack = debug_backtrace();
return (isset($stack[1+$Offset]) and isset($stack[1+$Offset]['type']) and $stack[1+$Offset]['type'] == '::');
}
/**
* isStandardCall
* checks the call-stack to see if its calling method is being called standardly
* @param $aOffset integer how many more steps back into the call stack should it step
* @author feyd
* @return boolean
*/
function isStandardCall($Offset=0) {
$stack = debug_backtrace();
return (isset($stack[1+$Offset]) and isset($stack[1+$Offset]['type']) and $stack[1+$Offset]['type'] == '->');
}
dbevfat wrote:It can be read from the result of debug_backtrace(), but I wouldn't rely on it, since it's a debugging function and might change when you least expect it ...
Why do you need that?
Cool...
Years of using PHP and i've never even known about these functions
However like you said, there is something about using a debug function in release code which leaves me feeling funny.
/**
* isStaticCall
* checks the call-stack to see if its calling method is being called statically
* @param $aOffset integer how many more steps back into the call stack should it step
* @author feyd
* @return boolean
*/
function isStaticCall($Offset=0) {
$stack = debug_backtrace();
return (isset($stack[1+$Offset]) and isset($stack[1+$Offset]['type']) and $stack[1+$Offset]['type'] == '::');
}
/**
* isStandardCall
* checks the call-stack to see if its calling method is being called standardly
* @param $aOffset integer how many more steps back into the call stack should it step
* @author feyd
* @return boolean
*/
function isStandardCall($Offset=0) {
$stack = debug_backtrace();
return (isset($stack[1+$Offset]) and isset($stack[1+$Offset]['type']) and $stack[1+$Offset]['type'] == '->');
}
Interestingly enough...I need this functionality for a framework (of sorts) i'm working on as well
Anyways, like mentioned above...I feel queezy about using DEBUG functions inside production code, but I just thought of an possible alternative...
Inside the function could you not just do something like:
One sloppy solution I see if setting a variable inside the constructor.. if the class was called normally set a variable, or not.. well do nothing.
When calling your class statically check to see whether that particular variable exists.
[feyd@home]>php -r "class foo{ static function bar() { return is_object($this); } } var_export(foo::bar());"
PHP Notice: Undefined variable: this in Command line code on line 1
Notice: Undefined variable: this in Command line code on line 1
false
[feyd@home]>php -r "class foo{ static function bar() { return is_object($this); } } var_export(foo::bar());"
PHP Notice: Undefined variable: this in Command line code on line 1
Notice: Undefined variable: this in Command line code on line 1
false
PHP 5 chokes eh....I kinda figured it would...I haven't tested it on 4 at all myself...so I dunno what would happen...but it's a scrap regardless cuz even if it did work on 4 it wouldn't be very portable code would it
however, this will only work at the current level of request.. the one I posted previously can see if any level of the call stack was either static or standard calling...