Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:22:33+00:00 2026-06-11T05:22:33+00:00

The problem is that the content of $oldpop get magically changed after executing the

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:22:35+00:00Added an answer on June 11, 2026 at 5:22 am

    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

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

    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);
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem that a statusbar get displayed over the content of the
I want to recreate the following header: The problem is that the content is
Here is my code: $('#right').load('textes.html #nicolas'); $('#right').load('textes.html #antoine'); The problem is that the content
The problem is that when the content loads it displays before the tag before
I'm currently facing the problem that I want to serve static content via a
Okay, my problem is that #main_content is the child of #content but it will
i have a problem that the content-length of header is undetermined ie -1 in
I'm writing an app that loads in content via ajax using the jQuery.load function.
I have the following scenario jQuery('#content').fadeOut('slow',function(){ jQuery('#content').load(detail.php?product=+imgID+&category=+cat); jQuery('#content').fadeIn('fast'); }); My problem however is that
Simple problem that I can't figure out... How can I print a '%' character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.