如何在PHP中使用两个变量调用静态方法

时间:2012-10-19 16:43:44

标签: php reflection static

我有两个变量:

$a = 'some_class';
$b = 'some_method';

我想做的是这样的事情(方法是静态的):

$a::$b;

有可能吗?我已经尝试过反射类,但我不能调用静态方法......

4 个答案:

答案 0 :(得分:3)

这应该这样做

call_user_func(array($a, $b));

答案 1 :(得分:2)

如何调用静态方法,3个选项

您有几个选择:

代码

<?PHP

    class test {
        static function doThis($arg) {
            echo '<br>hello world '.$arg;
        }
    }

    $class='test';
    $method='doThis';
    $arg='stack';

    //just call
    $class::$method($arg);

    //with function
    call_user_func(array($class, $method), $arg);

    //ugly but possible
    $command=$class.'::'.$method.'("'.$arg.'");';
    eval($command);

输出

    hello world stack
    hello world stack
    hello world stack

使用这些选项调用时会发生什么

带有回溯的代码,这样你就可以看到PHP内部会发生什么:

代码

<?PHP

class test {
    static function doThis($arg) {
        echo 'hello world with argument: '.$arg.PHP_EOL;
        print_R(debug_backtrace());
    }
}

function runTest() {

    $class='test';
    $method='doThis';
    $arg='stack';



    //just call
    $class::$method($arg);

    //with function
    call_user_func(array($class, $method), $arg);

    //ugly but possible
    $command=$class.'::'.$method.'("'.$arg.'");';
    eval($command);

}

echo '<pre>';
runTest();

输出

$类:: $方法($ ARG);

hello world with argument: stack
Array
(
    [0] => Array
        (
            [file] => folder/test.php
            [line] => 19
            [function] => doThis
            [class] => test
            [type] => ::
            [args] => Array
                (
                    [0] => stack
                )

        )

    [1] => Array
        (
            [file] => folder/test.php
            [line] => 31
            [function] => runTest
            [args] => Array
                (
                )

        )

)

call_user_func(array($ class,$ method),$ arg);

hello world with argument: stack
Array
(
    [0] => Array
        (
            [function] => doThis
            [class] => test
            [type] => ::
            [args] => Array
                (
                    [0] => stack
                )

        )

    [1] => Array
        (
            [file] => folder/test.php
            [line] => 22
            [function] => call_user_func
            [args] => Array
                (
                    [0] => Array
                        (
                            [0] => test
                            [1] => doThis
                        )

                    [1] => stack
                )

        )

    [2] => Array
        (
            [file] => folder/test.php
            [line] => 31
            [function] => runTest
            [args] => Array
                (
                )

        )

)

的eval($命令);

hello world with argument: stack
Array
(
    [0] => Array
        (
            [file] => folder/test.php(26) : eval()d code
            [line] => 1
            [function] => doThis
            [class] => test
            [type] => ::
            [args] => Array
                (
                    [0] => stack
                )

        )

    [1] => Array
        (
            [file] => folder/test.php
            [line] => 26
            [function] => eval
        )

    [2] => Array
        (
            [file] => folder/test.php
            [line] => 31
            [function] => runTest
            [args] => Array
                (
                )

        )

)

正如你所看到的那样,第一种方式在注册之间没有任何步骤,它直接进行呼叫,而其他两个选项自己作为一个功能并从自己进行呼叫。

在实践中没有太大的区别,但在优化这样的过程时可能有意义。

答案 2 :(得分:2)

你必须在var的末尾添加()才能转化为方法。 $ a :: $ b()不是$ a :: $ b;

<强> PHP

<?php

$a = 'some_class';
$b = 'some_method';
$c = 'double';

echo $a::$b();
echo "<br>";
echo $a::$c(15);


class some_class{

    public static function some_method(){
        return "static return";
    }

    public static function double($int){
        return $int*2;
    }
}

?>

<强>输出

static return
30

答案 3 :(得分:1)

这对您有用:$a::$b();

示例:

<?php

class A {
    public static function b()
    {
        echo 'Done!', PHP_EOL;
    }
}

$class  = 'A';
$method = 'b';

$class::$method(); // Shows:  Done!