The problem is that the content of $oldpop get magically changed after executing the function func, whereas the input of func is $matepop. Inside func, $oldpop is not used (I added the comment line to show the place – see the end of the code snippet of MAIN.PHP). Below I provide just some principal parts of the code. Maybe, someone could suggest the reason of the problem?
I should mention I don’t use static variables.
File MAIN.PHP
include_once 'func.php';
include_once 'select.php';
class Individual {
private $genes;
private $rank;
public function __construct() {
$this->genes = array();
$this->rank = 0;
}
public function setRank($val){
$this->rank = $val;
}
public function setGene($i,$val){
$this->genes[$i] = $val;
}
}
class Population {
private $ind;
public function __construct()
{
$this->ind = array();
}
public function addIndividual(Individual $ind)
{
$this->ind[] = $ind;
}
public function getIndividual($i){
return $this->ind[$i];
}
}
$oldpop = new Population();
for($i=0; $i<$popsize; $i++) {
$oldpop->addIndividual(new Individual());
}
$oldpop = func($oldpop,$popsize);
for ($i = 0; $i < $gener; $i++)
{
$matepop = new Population();
$matepop = nselect($matepop,$oldpop,$popsize);
// !!! Here the $oldpop content is correct (original)
$matepop = func($matepop,$popsize);
// !!!! Here the original content of $oldpop is magically changed
}
File SELECT.PHP
function nselect($matepop,$oldpop,$popsize) {
$select = array();
//...
$select[] = $oldpop->getIndividual($i);
//...
for ($i=0; $i < $popsize; $i++) {
$matepop->addIndividual($select[$i]);
}
return $matepop;
}
File FUNC.PHP
function func($pop,$popsize) {
//...
$pop->getIndividual($i)->setRank($val);
//...
return $pop;
}
PHP object variables don’t contain a full copy of the object; rather, they contain a reference to the object. This means that when you used
in your code, you didn’t make a new copy of the object
$i; you simply copied the reference to the object$iinto the array$select. This means that both$oldpopand$matepopcontain references to the same objects in their$indarrays. Then, when you later usedto set the rank of each
Individual-class object in$matepop, they also changed for$oldpop.