0

クラスインスタンスのコンテキストで、フォームの呼び出しは機能しないのに、フォームの$this->className::staticMethod呼び出しは$className::staticMethod機能するのはなぜですか?

以下の例では機能しますcallDoSomething2が、callDoSomething機能しません(パーサーエラーが発生します)。PHPバージョン5.3.15を使用しています。

<?php
class A {
    private $className;

    public function __construct($className) {
        $this->className = $className;
    }

    public function callDoSomething() {
        $this->className::doSomething();
    }

    public function callDoSomething2() {
        $className = $this->className;
        $className::doSomething();
    }
}

class B {
    public static function doSomething() {
        echo "hello\n";
    }
}

$a = new A('B');
$a->doSomething();
4

1 に答える 1

1

callDoSomething2 はそれを行う 1 つの方法です。もう 1 つは、次のような方法で何かを使用することです。

call_user_func("{$this->className}::doSomething");
于 2013-02-13T22:52:44.350 に答える