0

Is there a way to find out if a method is static or not?

My reason for needing to know: I call static methods outside of any instantiation context. Non-static methods can't be called then, since they don't make sense yet. I want to call them later, once instances of those classes exist.

When I call call_user_function_array($className.'::'.$functionName, $args); and the method is non-static, php seems to automatically create an instance of className and call that function. I want that call to FAIL for non-static functions.

DudeOnRock
  • 3,685
  • 3
  • 27
  • 58
  • "php seems to automatically create an instance of className and call that function" - it's not true. – sectus Apr 17 '13 at 01:56
  • @sectus: Why would those calls not fail then? – DudeOnRock Apr 17 '13 at 01:57
  • Use E_STRICT error level. – sectus Apr 17 '13 at 02:02
  • @sectus: stil is not reporting an error. I'll try to condense my code down to an example and post it. – DudeOnRock Apr 17 '13 at 02:05
  • "Calling non-static methods statically generates an E_STRICT level warning. " - [Static Keyword](http://www.php.net/manual/en/language.oop5.static.php) – sectus Apr 17 '13 at 02:07
  • @sectus: Of course you where right. I figured out why I wasn't getting that error. inside `__callStatic` i made another call to `call_user_function_array`, this time with an instance. Thanks! – DudeOnRock Apr 17 '13 at 02:28

3 Answers3

2

When I call call_user_function_array($className.'::'.$functionName, $args); and the method is non-static, php seems to automatically create an instance of className and call that function.

No, it doesn't. PHP isn't that automagic. No idea what you're doing there.

To call a method statically, you do exactly that:

call_user_func_array("$className::$functionName", $args);

To call a method of an object, you first need to explicitly instantiate an object, then call it like this:

$obj = new MyClass;
call_user_func_array(array($obj, $method), $args);

To programmatically figure out if a method is static or not, use ReflectionClass:

$r = new ReflectionClass($myClass);
$m = $r->getMethod($method);
var_dump($m->isStatic());

You should really know what a method is before you call it though, instead of dynamically trying to figure it out.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks for the warning at the end. Trust me, I know what I am doing... I know those are some famous last words.... ;-) – DudeOnRock Apr 17 '13 at 02:31
1

You can check methods with reflection.

class foo
    {
    static public function bar()
        {}

    public function baz()
        {}
    }

$reflection_class = new ReflectionClass('foo');

var_dump($reflection_class->getMethod('bar')->isStatic()); // boolean true
var_dump($reflection_class->getMethod('baz')->isStatic()); // boolean false

P.S. It's very weird that you trying to call methods but you do not know what they really are.

sectus
  • 15,605
  • 5
  • 55
  • 97
0

Since reflection is expensive I actually ended up calling set_error_handler with a callback that throws an ErrorException as described here to catch the warning when a static call was made to a non-static method.

Community
  • 1
  • 1
DudeOnRock
  • 3,685
  • 3
  • 27
  • 58
  • You *really* like spaghetti code, do you? To dynamically call methods you don't even know on objects you have no idea about you override the global error handler to catch exceptions...?! Sorry, but that boggles the mind. – deceze Apr 24 '13 at 01:47
  • I'm not saying anything for or against the PHP error handling system, you're right that it's not exactly great. I'm saying that you seem to be going to quite some lengths for something that seems like a dubious practice to begin with, and your code is probably going to be really hard to maintain or understand because of it. You're very much indulging in meta-programming there, which makes things harder to understand in almost any language, and you're doing it in a language that really isn't all that well suited for meta-programming to begin with. – deceze Apr 24 '13 at 04:53