views:

75

answers:

2

i have 2 functions in a class, in the first one i have something like that:

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

how to copy the contents from $test in the second function?

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

Sorry, but i have really no idea, how to make this :(

A: 

As soon as a function is finished, all the contents are deleted unless you assign it to $this->, or Global the variable.

So, in order to maintain the variable, you would need to do one of these:

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

Or, because you return the variable in func1, you could then pass it in to func2

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

you can do it in two ways:

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

In this case you are simply calling a function and storing the result into an array

OR

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

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

In this case func1 stores the array into the object attribute "test" while func2 prints it out

--
dam

dam
i'll try the second one, because i want to put some outputs into func1. thanks a lot.
cupakob
you are welcome. hope it helps
dam