2009-07-18 71 views
0

我有2個功能中的一類,在第一個我有類似的東西:訪問來自其他功能陣列中的類

public function func1() { 
$test = array(1,2,3); 
return $test; 
} 

如何從$測試的內容複製在所述第二功能?

public function func2() { 
$newarray = $this->func1->test; 
print_r($newarray); 
} 

抱歉,但我也實在沒有想法,如何使這個只要一個函數執行完畢,所有的內容被刪除,除非你把它分配被$ this->,或者:(

回答

0

你能做到這一點有兩種方式:

public function func2() {<br> 
    $newarray = $this->func1();<br> 
    print_r($newarray);<br> 
} 

在這種情況下,你只是調用函數,並將結果保存到一個數組

OR

public function func1() {<br> 
    $this->test = array(1,2,3);<br> 
} 

public function func2() {<br> 
    print_r($this->test);<br> 
} 

在這種情況下,func1將數組存儲到對象屬性「test」中,而func2將其打印出來

-

+0

我會嘗試第二個,因爲我希望把一些輸出到FUNC1。非常感謝。 – cupakob 2009-07-18 14:41:25

0

。全球變量

所以,爲了保持變量,你需要做的其中之一:

Global $test; 
$this->test = $test; 

或者,因爲你在FUNC1返回變量,然後你可以通過它來FUNC2

public function func1() 
{ 
    $test = array(1,2,3); 
    return $test; 
} 
public function func2($test) 
{ 
    // Do something 
}