2009-11-12 66 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 2009-11-12 20:54:36

+0

部分條款缺失的原因是我在離開辦公室的路上,所以我時間不夠。 – WAC0020 2009-11-13 13:09:13

回答

3

您需要致電newTest以使該方法內聲明的函數「可見」(請參見Functions within functions)。但那只是正常的功能而沒有任何方法。

173

試試這個:

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()',然後在類函數內部抓取結果?例如,我有一個查詢從表中選擇所有,然後返回一個提取所有結果集。是否有可能循環遍歷類函數中的結果集?例如'class query {public function show(){getResults();}}。 while($ stmt-> fetchCollumn()){ ECHO結果在這裏 }' – James111 2015-08-03 02:15:45

3

爲了有一個「函數內部功能」,如果我知道你要問什麼,你需要PHP 5.3,在那裏你可以利用新的閉幕優勢特徵。

所以你可以有:

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

您提供的樣品是無效的PHP和有幾個問題:

public scoreTest() { 
    ... 
} 

不是一個正確的函數聲明 - 你需要聲明功能與'功能'關鍵字。

的語法而應是:

public function scoreTest() { 
    ... 
} 

二,包裹bigTest()和smallTest()公共函數的函數(){}並不能讓他們的私人 - 你應該使用專用的關鍵字上都這些是單獨的:

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; 
    } 
} 

另外,在類聲明('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(); 

例題

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 2013-12-04 20:10:58

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; 
    } 
} 
相關問題