Is there any way I can check if a method is being called statically or on an instantiated object?
Check whether $this
is defined
Create a static variable and change it in the constructor.
private static $isInstance = false;
public function __construct()
{
self::$isInstance = true;
}
Now you can check it
public function myMethod()
{
if (self::$isInstance) {
// do things
}
}
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.
<?php
class MethodTest
{
public function __call($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
/** As of PHP 5.3.0 */
public static function __callStatic($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
$obj = new MethodTest;
$obj->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
Testing isset($this) wasn't working for me, as mentioned by troelskn "$this will be set as the callers context."
abstract class parent
{
function bar()
{
if( isset( $this ) ) do_something();
else static::static_bar();
}
function static static_bar()
{
do_something_in_static_context();
}
}
class child extends parent
{
...
}
$foo = new child();
$foo->bar(); //results in do_something()
child::bar(); //also results in do_something()
In my case, I have a parent class with a object and static context function that performs the same task within a child class. isset( $this ) was always returning true, however, I noticed that while $this switches between being class child in object context and calling(?) class on static context, the wonderful __class__
magic constant, remained as class parent!
Shortly there-after finding the function is_a, we can now test if we're in static context:
if( is_a($this, __CLASS__) ) ...
Returns true on object context, false on static context.
Please test for your own implementations, as I'm only testing this for my specific scenario (a unique case of inheritance calling) in 5.3.
Unfortunately (for my case) I am yet unable to find a way to call the static_bar()
since $this and static are referring to a separate class, and __class__
is referring to the parent class. What I need is a way to call child::static_bar()...
Try the following:
class Foo {
function bar() {
$static = !(isset($this) && get_class($this) == __CLASS__);
}
}
Source: seancoates.com via Google
"Digging it out of debug_backtrace()" isn't too much work. debug_backtrace() had a 'type' member that is '::' if a call is static, and '->' if it is not. So:
class MyClass {
public function doStuff() {
if (self::_isStatic()) {
// Do it in static context
} else {
// Do it in object context
}
}
// This method needs to be declared static because it may be called
// in static context.
private static function _isStatic() {
$backtrace = debug_backtrace();
// The 0th call is to _isStatic(), so we need to check the next
// call down the stack.
return $backtrace[1]['type'] == '::';
}
}