0

私のすべてのウェブサイトは、URLやファイルの場所などを扱う共通のスターターを共有しています。処理する必要があるのは、ディレクトリ、ファイルが存在する、ファイルが存在しないという3つのケースです。各アプリケーションには、ケースごとに固有のコードがあります。私はrunkitを少しいじくり回すことに決め、コードを統一しようとしています。各ケースは、runkitを介して再定義できる関数によって処理されます。

このコードを考えてみましょう:

class start {
   function __construct() {
       $this->options = array();
   }

   public function process() {
       // some code here
       $this->file_not_exists();
   }

   public function file_not_exists() {
       $this->options['page'] = 222;
   }

   public function redefine($what, $code) {
       runkit_method_redefine(get_class($this), $what, '', $code, RUNKIT_ACC_PUBLIC);
   }
}

$start = new start();

$start->redefine('file_not_exists', '$this->options["page"] = 333;')

//  page is now 333

この部分は意図したとおりに機能します。しかし、再定義されたメソッドがユーザー関数を呼び出すようにコードを変更しようとすると、機能します。しかし、神の愛のために、$thisを関数に渡す方法がわかりません。

再定義メソッドは次のようになります。

public function redefine($what, $code) {
    runkit_method_redefine(get_class($this), $what, '', 'call_user_func('.$code.'(), '.$this.');', RUNKIT_ACC_PUBLIC)
}

これは、私が何を試しても機能しません(call_user_func_arrayも同様です)。私はそれを理解することはできません。記録のために:

public function redefine($what, $code) {
    my_user_function($this);
}

動作します。

どんな助けでも大歓迎です。

これは単なる実験であり、これを行う方法を知りたいことに注意してください:)

編集:私は得る:

Catchable fatal error: Object of class starter could not be converted to string in blablallala\rewrite_starter2.php on line 153
4

2 に答える 2

0

{...不要なため削除...}

====編集=====

[新しい問題の場合、必要なのはこれです]

<?
class start {
   function __construct() {
       $this->options = array();
   }

   public function process() {
       // some code here
       $this->file_not_exists();
   }

   public function file_not_exists() {
       $this->options['page'] = 222;
   }

   public function redefine($what, $code) {
       runkit_method_redefine(get_class($this), 
       $what, 
       '', 
       'call_user_func(array($this,' .$code. '),$this);', 
       RUNKIT_ACC_PUBLIC);
   }

   public function my_func($someobj)
   {
        print_r($someobj);
   }
}


$start = new start();
$start->redefine('file_not_exists', 'my_func');

$start->process();

?>
于 2011-01-31T10:13:50.013 に答える
0

関数のドキュメントにはcall_user_func、最初の引数は「呼び出し可能」であると記載されています。したがって、クラスメソッドを動的に呼び出すには、を渡す必要がありarray($obj, 'func_name')ます。

于 2012-09-09T20:13:25.387 に答える