I'm having this code here and no matter what I do all the items in the array are the same when I do a var_dump(). I don't know why I can't loose the reference and what to do about it.
Here is a code simplification replicating the same behaviour
class Numbr {
public $value = NULL;
function __construct(int $value) {
$this->value = $value;
}
public function multiply(int $multiplier) {
$this->value *= $multiplier;
}
}
class Test {
public $operations = [];
function __construct($values) {
$operations = [];
foreach($values AS $value) $operations[] = new Numbr($value);
$this->operations = $operations;
}
public function simulation() {
$ops = $this->operations;
//Here I make 4 copies of $ops that I want to end up being different
$op1 = $this->transformation($ops, 2);
$op2 = $this->transformation($ops, -2);
$op3 = $this->transformation($ops, -1);
$op4 = $this->transformation($ops, 4);
$this->operations = [$op1, $op2, $op3, $op4];
}
public function transformation(array $ops, int $value) {
$output = [];
foreach($ops AS $N) {
$N->multiply($value);
$output[] = $N;
}
return $output;
}
}
$Test = new Test([1,2,3,4]);
$Test->simulation();
var_dump($Test)