2009-11-12 8 views
82

私はこれをやり遂げる方法を見つけ出そうとしていますが、どうしたらよいか分かりません。ここでClassメソッド内で関数を呼び出す?

は私がしようとしているものの例である:ここで

class test { 
    public newTest(){ 
      function bigTest(){ 
       //Big Test Here 
      } 
      function smallTest(){ 
       //Small Test Here 
      } 
    } 
    public scoreTest(){ 
      //Scoring code here; 
    } 
} 

は、私は)私はbigTest(呼ぶのですか、との問題があります一部ですか?

+2

を実行します:関数とメソッドは、メソッド===まったく同じ機能です。メソッドという用語は、クラスの機能を記述するために、より多くの場合OO言語で使用されます。 – markus

+0

いくつかの用語が欠落している理由は、私がオフィス外に出ていたため、私は短い時間でした。 – WAC0020

答えて

3

このメソッド内で宣言されている関数を「可視」にするには、newTestを呼び出す必要があります(Functions within functions参照)。しかし、それはちょうど正常な機能であり、方法はありません。

173

このお試しください:私はあなたが求めているものを理解していれば、あなたが新しい閉鎖を活用することができますPHP 5.3を必要とする、「関数内で機能」を持つために

class test { 
    public function newTest(){ 
      $this->bigTest(); 
      $this->smallTest(); 
    } 

    private function bigTest(){ 
      //Big Test Here 
    } 

    private function smallTest(){ 
      //Small Test Here 
    } 

    public function scoreTest(){ 
      //Scoring code here; 
    } 
} 

$testObject = new test(); 

$testObject->newTest(); 

$testObject->scoreTest(); 
+1

クラスファンクション内の別の.phpページから 'function()'を実行し、クラスファンクション内で結果を取得することはできますか?たとえば、テーブルからすべてを選択してからすべての結果セットをフェッチするクエリがあります。その結果セットをクラス関数の中でループすることは可能ですか?例えば、クラスクエリ{public function show(){ getResults(); while($ stmt-> fetchCollumn()){ ECHO RESULTS HERE } ' – James111

3

を特徴。

は、あなたが持っている可能性があり:

public function newTest() { 
    $bigTest = function() { 
     //Big Test Here 
    } 
} 
16

をあなたが提供されているサンプルは、有効なPHPでなく、いくつかの問題があります。

public scoreTest() { 
    ... 
} 

は、適切な関数宣言ではありません - あなたは、関数を宣言する必要があります'function'キーワードを使用します。

構文は、むしろ次のようになります。

public function scoreTest() { 
    ... 
} 

第二に、彼らは民間のことはありませんbigTest()とsmallTest()パブリック関数内の関数を(){}包む - あなたは両方でプライベートキーワードを使用する必要があります

また、クラス宣言( 'Test')でクラス名を大文字にするのが一般的です。

希望に役立ちます。 (文の新しい付き)クラスからインスタンス化されたオブジェクトの任意のメソッドを呼び出すために

0

例1

class TestClass{ 
public function __call($name,$arg){ 
call_user_func($name,$arg); 
} 
} 
class test { 
    public function newTest(){ 

      function bigTest(){ 
       echo 'Big Test Here'; 
      } 
      function smallTest(){ 
       echo 'Small Test Here'; 
      } 

$obj=new TestClass; 

return $obj; 
    } 

} 
$rentry=new test; 
$rentry->newTest()->bigTest(); 

例2

class test { 
    public function newTest($method_name){ 

      function bigTest(){ 
       echo 'Big Test Here'; 
      } 
      function smallTest(){ 
       echo 'Small Test Here'; 
      } 

     if(function_exists($method_name)){  
call_user_func($method_name); 
     } 
     else{ 
      echo 'method not exists'; 
     } 
    } 

} 
$obj=new test; 
$obj->newTest('bigTest') 
+0

$ rentry-> newTest() - > bigTest(); $ rentry-> newTest() - > smallTest(); ---- 致命的なエラー:bigTest()を再宣言できません(以前宣言されています)。 – tfont

4

、あなたはそれを "ポイント" する必要があり。外側からは、新しいステートメントで作成されたリソースを使用します。 newによって作成されたオブジェクトPHP内で、同じリソースを$ this変数に保存します。 したがって、クラス内では$ thisによってメソッドを指さなければなりません。あなたのクラスで 、クラス内からsmallTestを呼び出すために、あなただけ書き、実行したい新しいステートメントによって作成されたすべてのオブジェクトのPHPを伝える必要があります。

$this->smallTest(); 
6

私はあなたが何かを探していると思いますこれです。

class test { 

    private $str = NULL; 

    public function newTest(){ 

     $this->str .= 'function "newTest" called, '; 
     return $this; 
    } 
    public function bigTest(){ 

     return $this->str . ' function "bigTest" called,'; 
    } 
    public function smallTest(){ 

     return $this->str . ' function "smallTest" called,'; 
    } 
    public function scoreTest(){ 

     return $this->str . ' function "scoreTest" called,'; 
    } 
} 

$test = new test; 

echo $test->newTest()->bigTest(); 
1

あなたは現在のクラスの静的変数や関数を呼び出すしたい場合は、self::CONSTの代わり$this->CONSTを使用することができます。

2
class sampleClass 
    { 
     public function f1() 
     { 
      return "f1 run"; 
     } 

     public function f2() 
     { 
      echo ("f2 run"); 
      $result = $this->f1(); 
      echo ($result); 
     } 

    f2(); 

    } 

出力:

f2の実行 f1は念のために

1
class test { 
    public newTest(){ 
     $this->bigTest(); 
     $this->smallTest(); 
    } 

    private function bigTest(){ 
     //Big Test Here 
    } 

    private function smallTest(){ 
     //Small Test Here 
    } 

    public scoreTest(){ 
     //Scoring code here; 
    } 
} 
関連する問題