2009-07-18 61 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 
}