1

私には2つの変数があります:

$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
                (
                )

        )

)

eval($ command);

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

メソッドに変換するには、変数の最後に()を追加する必要があります。$ 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 に答える