9

Is it possible to use the __call magic method when calling functions statically?

3 Answers 3

16

Not yet, there is a proposed (now available) __callStaticDocs method in the pipeline last I knew. Otherwise __call and the other __ magic methods are not available for use by anything but the instance of a object.

Sign up to request clarification or add additional context in comments.

Comments

9

You have to use the other magic method, __callStatic - this is only available in PHP >= 5.3, which hasn't actually been released yet.

4 Comments

Import to note that's not available yet.
Ah bugger - I was wondering about this, and started to write the question... then I found __callStatic but didn't realise it won't be available until php 5.3
I haven't been as excited for a "minor" release in PHP since...Yeah PHP5.3 gives me hope for the language.
I know this is an old question, but I would just like to point out that __callStatic is available >= 5.3 (Not > 5.3) Not sure if anyone else was confused by that, but I was.
0

As described before, there is no magic static caller. But you can code like this:

   class First {
        public static function test1(){
            return 1;
        }
        public static function test2(){
            return 2;
        }
    }


    class Second {
        public static function test1(){
            if(func_num_args()>0){
                return func_get_args();
            }
            return 21;
        }
        public static function test2(){
            return 22;
        }
    }

    class StaticFactory {
        public static function factory($class, $method){
            if(func_num_args()>2){
                $args = func_get_args();
                array_shift($args);
                array_shift($args);
                return call_user_func_array(array($class,$method), $args);
            }else{
            return call_user_func_array(array($class,$method), array());
            }
        }
    }

    print_r(StaticFactory::factory("Second", "test1", 1, false, true));

    print_r(StaticFactory::factory("First", "test1"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.