CODE - 执行函数后,对象的内容会更改 [英] CODE - The content of an object is changed after executing a function

查看:114
本文介绍了CODE - 执行函数后,对象的内容会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是 $ oldpop 的内容在执行函数 func 后会发生神奇的变化, func $ matepop 。内部 func $ oldpop 不使用(我添加了注释行以显示地方 - MAIN.PHP 的代码段)。下面我只提供代码的一些主要部分。也许有人可以提出问题的原因?

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.

档案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

    }

文件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;
}

文件FUNC.PHP b
$ b

File FUNC.PHP

function func($pop,$popsize) { 
  //...
  $pop->getIndividual($i)->setRank($val);
  //...
  return $pop;
}


推荐答案

PHP对象变量不包含对象的完整副本; 这意味着当你使用

$select[] = $oldpop->getIndividual($i);

在代码中,您没有创建对象的新副本 $ i ;您只需将对象 $ i 的引用复制到数组 $ select 中。这意味着 $ oldpop $ matepop 都包含对其中的相同 $ ind 数组。然后,当您以后使用

in your code, you didn't make a new copy of the object $i; you simply copied the reference to the object $i into the array $select. This means that both $oldpop and $matepop contain references to the same objects in their $ind arrays. Then, when you later used

$pop->getIndividual($i)->setRank($val);

设置每个个人 $ matepop 中的类对象,它们也更改为 $ oldpop

to set the rank of each Individual-class object in $matepop, they also changed for $oldpop.

这篇关于CODE - 执行函数后,对象的内容会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆