0

I want to use __callStatic as a preProcessor for calling static methods. My idea is to make the methods private so every static call is forwarded to __callStatic. Then I could use this to do some stuff and call the method then. But it seems not possible. Here is an example:

class A {

    public static function __callStatic($name, $params) {
        var_dump($name);

        // TODO call the private function from class B here

        //call_user_func_array('self::' . $name, $params); //infinite loop

    }

}

class B extends A {

    private static function test($bar) {
        echo $bar;
    }

}

B::test('foo');

Perhaps somebody is having a solution :-)

Christian Koch
  • 701
  • 7
  • 16

3 Answers3

1

This works too

class A 
{
    public static function __callStatic($method, $params)
    {
        return call_user_func_array('static::'.$method, $params);
    }
}
class B extends A
{
    protected static function test($value)
    {
        echo $value;
    }
}
B::test('foo');

The first problem with your original is making your methods private. Private methods are only in scope for current class (in this case B::test()), however, the method is called from A::__callStatic() and so is out of scope.

The second issue is use of self:: although I can't offer an adequate explanation why I'm afraid (perhaps someone more versed in the nuances might shed some light?), but replacing self with the static keyword works.

Crisp
  • 11,417
  • 3
  • 38
  • 41
  • Your solution is really smart. Thank you. I've never read about the static keyword :-( – Christian Koch May 20 '12 at 08:34
  • If you never read about the static keyword then how could you call a static function? You more than likely had the wrong intent in the question posted. None the less this keyword will help I suppose, not much difference to me as long as the function was not defined in a class to begin with and even if so there is probably a way just not in PHP directly :P – Jay May 20 '12 at 14:51
  • I meant 'static::' in call_user_func_array('static::'.$method, $params); – Christian Koch May 20 '12 at 17:38
0

This works

    <?php
/**
 * Created by JetBrains PhpStorm.
 * User: ckoch
 * Date: 19.05.12
 * Time: 10:43
 * To change this template use File | Settings | File Templates.
 */

class A {

    public static function __callStatic($name, $params) {
        var_dump($name);

        // TODO call the private function from class B here

        //call_user_func_array('self::' . $name, $params); //infinite loop

        //forward_static_call_array(array(self, $name), $params); // loops too

        $method = new ReflectionMethod(get_called_class(), $name);
        $method->setAccessible(true);
        $method->invokeArgs(null, $params);

    }

}

class B extends A {

    private static function test($bar) {
        var_dump($bar);
    }

}

B::test('foo');
Christian Koch
  • 701
  • 7
  • 16
0

http://www.php.net/manual/en/function.get-defined-functions.php ? see a example @ http://plugins.svn.wordpress.org/fresh-post/trunk/phpthumb.functions.php

Jay
  • 3,276
  • 1
  • 28
  • 38
  • 1
    It don't understand the context to my question? – Christian Koch May 20 '12 at 08:30
  • If you can get the defined function would it not be very easy to call? This also is less overhead than using reflection because you are not looking for the constructor and related methods. – Jay May 20 '12 at 14:49