2

プライベート ディスパッチ テーブルを持つクラスがあるとします。

$this->dispatch = array(
    1 => $this->someFunction,
    2 => $this->anotherFunction
);

私が電話したら

$this->dispatch[1]();

メソッドが文字列ではないというエラーが表示されます。次のような文字列にすると:

$this->dispatch = array(
    1 => '$this->someFunction'
);

これにより 致命的なエラーが発生します: Call to undefined function $this->someFunction()

私も使用してみました:

call_user_func(array(SomeClass,$this->dispatch[1]));

メッセージの結果: call_user_func(SomeClass::$this->someFunction) [function.call-user-func]: First argument is expected to be a valid callback .

編集: $this が SomeClass のときに SomeClass::$this を呼び出しているため、これはあまり意味がないことに気付きました。配列を含むいくつかの方法でこれを試しました

array($this, $disptach[1])

これはまだ私が必要とするものを達成していません。

編集を終了

これは、クラスがなく、いくつかの関数を含むディスパッチ ファイルがある場合に機能します。たとえば、これは機能します:

$dispatch = array(
    1 => someFunction,
    2 => anotherFunction
);

これらをクラスのプライベートメソッドとして保持しながら、ディスパッチテーブルで使用できる方法があるかどうか疑問に思っています。

4

2 に答える 2

9

メソッドの名前は、次のようにディスパッチに保存できます。

$this->dispatch = array('somemethod', 'anothermethod');

次に使用します:

$method = $this->dispatch[1];
$this->$method();
于 2008-11-20T18:17:14.753 に答える
5

関数の call_user_func*-Family は次のように動作する必要があります。

$this->dispatch = array('somemethod', 'anothermethod');
...
call_user_func(array($this,$this->dispatch[1]));
于 2008-11-20T18:31:10.547 に答える