That's gonna be a tough one to explain.
I have a class Tree which is rather complex, I try to simplify:
class Tree {
/**
* @var Node[]
*/
private $nodes;
/**
* @var Edge[]
*/
private $edges;
}
class Node {
/**
* @var Value[]
*/
private $values;
/**
* @var array
*/
private $someArray;
}
class Value {
/**
* @var float
*/
private $result;
}
So you can see I have an object Tree containing two arrays of Objects again (Node and Edge) and every Node has an array of objects (Value), some other 'simple array' and every Value has a property result.
To calculate the property result I basically need to run up and down my tree etc... so some have business logic which will end up in still the same Tree but having some calculated results for my nodes.
So what I do so far is something like:
$tree = myCalculationFunction($tree, $calcParameter);
return $tree->getNode(1)->getValue(1)->getResult();
But no when I call additionally the same function with a different calcParameter of course my Tree operates on referenced Nodes, Values etc.
So I cannot:
$initialTree = myCalculationFunction($tree, $calcParameter);
$treeOne = myCalculationFunction($initialTree, $calcParameterOne);
$treeTwo = myCalculationFunction($initialTree, $calcParameterTwo);
$result1 = $treeOne->getNode(1)->getValue(1)->getResult();
$result2 = $treeTwo->getNode(1)->getValue(1)->getResult();
So by I have no deep-copy of my $initialTree because all objects in it are byReference. I cannot clone and I don't see how some manual/custom deep-copy like here will work for this case.
How can I achieve this here? I basically need the initialTree to be stable and every calculation function call manipulates a fully copy of the initially 'calculated tree'.