2013-08-06 26 views
0

我想要一個函數,然後多次使用不同的參數。獲取多次調用函數時添加的信息

例如:

<?php 
    class Test { 
     var $test; 

     public function func($val) { 
     $this->test = $val; 
     } 

     public function buildFunc() { 
      if(!empty($this->test)) { 
       $ret = $this->test; 
      } 
      return $ret; 
     } 
    } 

?> 

然後調用頁面上:

$test = new Test; 
$test->func("test1"); 
$test->func("test2"); 

echo $test->buildFunc(); 

然後打印屏幕上的TEST2。我希望它能打印出它們兩個。

+0

請改用數組? – hjpotter92

+0

不清楚你想要什麼。你的函數調用如何? –

+0

你想要$ test-> buildFunc();打印'test1和test2'? – knightrider

回答

3

要麼創建您的對象的2個實例;

$test1 = new Test; 
$test1->func("test1"); 
$test2 = new Test; 
$test2->func("test2"); 

echo $test1->buildFunc(); 
echo $test2->buildFunc(); 

或者測試一個數組;

class Test { 
    var $test = array(); 

    public function func($val) { 
    $this->test[] = $val; 
    } 

    public function buildFunc() { 
     return print_r($this->test, true); 
    } 
} 
1

可能是你的意思是你想存儲所有的值?然後使用一個數組:

public function func($val) { 
    $this->test[] = $val; 
    } 

    public function buildFunc() { 
     return $this->test 
    } 

然後使用結果與數組一起使用。

0

推變量數組

<?php 
    class Test { 
     var $test; 

     public function __construct(){ 
     $this->test=array();//Declare $test as an array 
     } 
     public function func($val) { 
     $this->test[]=$val;//Push to array 
     } 

     public function buildFunc() { 
      if(!empty($this->test)) { 
       $ret = implode(",",$this->test); 
      } 
      return $ret; 
     } 
    } 

?> 
0

嗯..你的代碼不正是你告訴它做的事。考慮情況時,你有沒有OOP:

$str = 'test 1'; 
$str = 'test 2'; 

echo $str; //prints test 2 

所以你需要單獨迴應他們,如果它不會是一個面向對象的情況。

$test = new Test; 

$test->func("test1"); 
echo $test->buildFunc(); 

$test->func("test2"); 
echo $test->buildFunc(); 
0

當調用方法創建2個測試對象實例時。

$test = new Test; 
$test->func("test1"); 
echo $test->buildFunc(); 

$test2 = new Test; 
$test2->func("test2"); 
echo $test2->buildFunc(); 

如果你不想創建2個實例,你必須改爲創建一個數組。

0

當你說都你的意思是這樣

test1test2

還是要

test1的 test2的

對於你可以將字符串的第一個選項:

<?php 
    class Test { 
     var $test; 

     public function func($val) { 
      $this->test = $test . $val; <-- add val to the end 
     } 

     public function buildFunc() { 
      if(!empty($this->test)) { 
       $ret = $this->test; 
      } 
      return $ret; 
     } 
    } 
?> 

對於第二個:

<?php 
    class Test { 
     var $test = array(); 

     public function func($val) { 
      $this->test[] = $val; <-- add val to 
     } 

     public function buildFunc() { 
      if(!empty($this->test)) { 
       foreach($test as $item){ 
       echo $item . "<br/>"; 
       } 
      } 
     } 
    } 
?> 
+0

'foreach'應該在'buildFunc()'裏面,代替'return $ ret;'爲你的班級工作 –

+0

Woops謝謝!更新:-) –

+0

這將無法正常工作。 –

0

如何創建一個構造函數並初始化測試的值並對第二個值進行連接。

<?php 
    class Test { 
     var $test; 

     public function __construct($init){ 
      $this->test = $init; 
     } 

     public function func($val) { 
     $this->test .= $val; 
     return $this; 
     } 

     public function buildFunc() { 

      if(!empty($this->test)) { 
       $ret = $this->test; 
      } 
      return $ret; 
     } 
    } 

$test = new Test("test1"); 
$test->func("test2"); 


echo $test->buildFunc(); 

?>