2009-11-12 8 views
82

मैं यह जानने की कोशिश कर रहा हूं कि ऐसा करने के बारे में कैसे जाना है, लेकिन मुझे पूरा यकीन नहीं है कि कैसे।क्लास विधि के भीतर किसी फ़ंक्शन को कॉल करना?

यहाँ मैं क्या करना है कोशिश कर रहा हूँ का एक उदाहरण है:

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

यहाँ भाग मैं के साथ समस्या हो रहा है, मैं कैसे कहते हैं bigTest()?

+2

चलाएँ: एक समारोह और एक विधि बिल्कुल एक ही समारोह विधि है ===। कक्षा के कार्य का वर्णन करने के लिए ओओ भाषा में शब्द विधि का अधिक उपयोग किया जाता है। – markus

+0

कारणों में से कुछ शब्द गायब हैं कारण मैं कार्यालय से बाहर निकल रहा था, इसलिए मैं समय पर छोटा था। – WAC0020

उत्तर

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 पृष्ठ से 'फ़ंक्शन() 'को चलाने के लिए संभव है और फिर क्लास फ़ंक्शन के अंदर परिणाम प्राप्त करें? उदाहरण के लिए मेरे पास एक क्वेरी है जो किसी तालिका से सभी का चयन करती है और उसके बाद सभी परिणाम सेट प्राप्त करती है। क्या कक्षा परिणाम के अंदर उस परिणाम सेट के माध्यम से लूप करना संभव है? उदाहरण के लिए 'वर्ग क्वेरी {सार्वजनिक फ़ंक्शन शो() { getResults(); जबकि ($ stmt-> fetchCollumn()) { ECHO परिणाम यहां } ' – James111

3

आदेश में एक "एक समारोह के भीतर समारोह" करने के लिए, अगर मैं समझता हूँ कि तुम क्या कह रहे हैं, तो आप पीएचपी 5.3, जहाँ आप नए बंद का लाभ ले सकते जरूरत सुविधा।

तो तुम हो सकता है:

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

नमूना आपके द्वारा दी गई वैध पीएचपी नहीं है और कुछ मुद्दों है:

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

साथ ही, यह कक्षा घोषणाओं ('टेस्ट') में कक्षा के नामों को पूंजीकृत करने के लिए सम्मेलन है।

उम्मीद है कि मदद करता है।

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(); 

example2

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

$ किराया-> newTest() -> bigTest(); $ किराया-> newTest() -> smallTest(); ---- घातक त्रुटि: bigTest() पहले घोषित नहीं किया जा सकता है। – tfont

4

एक वस्तु एक वर्ग (नया बयान के साथ) से instantiated के किसी भी विधि कॉल करने के लिए, आप के लिए यह करने के लिए "बिंदु" की जरूरत है । बाहर से आप केवल नए कथन द्वारा बनाए गए संसाधन का उपयोग करें। किसी भी ऑब्जेक्ट के अंदर PHP द्वारा नए बनाए गए, उसी संसाधन को $ इस चर में सहेजता है। तो, कक्षा के अंदर आपको इस विधि को $ इस पर इंगित करना होगा। अपनी कक्षा में, वर्ग के अंदर से 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; 
    } 
}