Salim Posted January 3, 2008 Share Posted January 3, 2008 Hi, I have a strange problem, but I really need to figure out how this works. abstract class A { function A() { echo "constr. A"; } static abstract function foo(); } class B extends A { function B() { echo "constr. B"; } static function foo() { echo "B::foo"; } } class C extends A { function C() { echo "constr. C"; } static function foo() { echo "C::foo"; } } function getClName() { return C; } $clname = getClName(); // $obj = new $clname(); // this would work $clname::foo(); // this does not work! how can I make this work? I think you can see my problem. I want to call a static method of a class I don't know. It works perfectly when constructing an object and calling its non-static methods. But how can I call its static methods without creating an object from it? greetings, Salim Quote Link to comment https://forums.phpfreaks.com/topic/84332-solved-how-do-i-call-a-static-method-from-an-unknown-class/ Share on other sites More sharing options...
Zane Posted January 3, 2008 Share Posted January 3, 2008 try returning a new C instance function getClName() { return new C; } Quote Link to comment https://forums.phpfreaks.com/topic/84332-solved-how-do-i-call-a-static-method-from-an-unknown-class/#findComment-429509 Share on other sites More sharing options...
Salim Posted January 3, 2008 Author Share Posted January 3, 2008 No it doesn't work. Anyway, what if the constructor expects parameters? In that case it wouldn't work when creating an object before. Quote Link to comment https://forums.phpfreaks.com/topic/84332-solved-how-do-i-call-a-static-method-from-an-unknown-class/#findComment-429542 Share on other sites More sharing options...
duclet Posted January 3, 2008 Share Posted January 3, 2008 I don't think PHP currently has support for that. Quote Link to comment https://forums.phpfreaks.com/topic/84332-solved-how-do-i-call-a-static-method-from-an-unknown-class/#findComment-429793 Share on other sites More sharing options...
deadimp Posted January 4, 2008 Share Posted January 4, 2008 You can use the new object construction as zanus suggested and just use the -> operator, as long as you don't put the static keyword in front of your method declaration. PHP is lenient on calling functions statically. You can call any function using the static member operator :: and it'll work up until a $this reference. That gets a little awkward though, and you've already used the static keyword. Another way would be to look at call_user_func() and see how you can format callback: mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] ) function - The function to be called. Class methods may also be invoked statically using this function by passing array($classname, $methodname) to this parameter. So in this context: $class=getClName(); //Returns a string "C" call_user_func(array($class,"foo")); //Calls C::foo() You can add whatever parameters you want. Also look at call_user_func_array() if you want some more flexiblity. EDIT: Fixed the error Anthop pointed out. Otherwise it would have executed C("foo") instead of C::foo(). Quote Link to comment https://forums.phpfreaks.com/topic/84332-solved-how-do-i-call-a-static-method-from-an-unknown-class/#findComment-429868 Share on other sites More sharing options...
Anthop Posted January 4, 2008 Share Posted January 4, 2008 deadimp's suggestion seems very promising. Also, I'm not sure, but I think to correctly call a static function, C::foo() in this case, you would have to actually use: call_user_func(array($class, "foo")); Quote Link to comment https://forums.phpfreaks.com/topic/84332-solved-how-do-i-call-a-static-method-from-an-unknown-class/#findComment-430102 Share on other sites More sharing options...
Salim Posted January 4, 2008 Author Share Posted January 4, 2008 Thanks a lot. This works fine. Although $clname::foo(); would have been much nicer (what a pity it doesn't work), call_user_func helps. Quote Link to comment https://forums.phpfreaks.com/topic/84332-solved-how-do-i-call-a-static-method-from-an-unknown-class/#findComment-430330 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.