1

我有两个变量:

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

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

$a::$b;

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

4

4 回答 4

3

这应该这样做

call_user_func(array($a, $b));
于 2012-10-19T16:47:00.860 回答
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();

输出

$class::$method($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
                (
                )

        )

)

评估($命令);

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 个选项本身作为一个函数并从它们自己进行调用。

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

于 2012-10-19T16:50:23.303 回答
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
于 2012-10-19T16:51:10.063 回答
1

这将为您工作:$a::$b();

例子:

<?php

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

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

$class::$method(); // Shows:  Done!
于 2012-10-19T16:47:36.100 回答