1

さて、私はほとんどのMVCがどのように機能するかを持っています

いくつかのサイト/クラス名/クラス関数/関数

class test(){
  public function test2(){
    // action will be 'function' in adress
    $action = $this->action ? $this->action : array($this, 'test3');
    function test3(){
      print 1;
    }
    $action();
  }
}

したがって、実行somesite/test/test2/test3すると '1' が出力されますが、実行somesite/test/test2/phpinfoすると phpinfo が表示されます。
質問: クラス関数内の関数の存在を確認する方法は?

UPD
phpinfo を忘れないでください。function_exists で表示されます。method_existsはクラス関数で検索しますが、クラス関数
UPDの関数では検索しません

class test{
    public function test2(){
    // site/test/test2/test3
        $tmpAction = $this->parenter->actions[1]; // test3  
        $test3 = function(){
            print 1;
        };
        if(isset($$tmpAction)){
            $$tmpAction();
        }else{
            $this->someDafaultFunc();
        }
    }
}
4

3 に答える 3

2

クラス内の特定のメソッドが存在するかどうかを確認するには、http: //php.net/method-existsを使用します。

   $c = new SomeClass();
   if (method_exists($c, "someMethod")) {
       $c->someMethod();
   }

クラス名を使用することもできます。

   if (method_exists("SomeClass", "someMethod")) {
       $c = new SomeClass();
       $c->someMethod();
   }

問題を「修正」するにはtest3()、クラス メソッドを作成します。

class test(){
  private function test3() {
      print 1;
  }
  public function test2(){
    // action will be 'function' in adress
    $action = $this->action ? $this->action : array($this, 'test3');

    if (method_exists($this, $action)) {
        $this->$action();
    } else {
        echo "Hey, you cannot call that!";
    }
  }
}
于 2013-01-16T08:11:11.737 に答える
2

http://php.net/function-exists

http://php.net/method-exists

if ( function_exists('function_name') ) {
    // do something
}

if ( method_exists($obj, 'method_name') ) { /* */ }

魔法のメソッド__call()もチェックしてください。

于 2013-01-16T08:10:02.183 に答える
2
class test{
    public function test2(){
    // site/test/test2/test3
        $tmpAction = $this->parenter->actions[1]; // test3  
        $test3 = function(){
            print 1;
        };
        if(isset($$tmpAction)){
            $$tmpAction();
        }else{
            $this->someDafaultFunc();
        }
    }
}
于 2013-01-16T08:46:12.647 に答える