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 8186989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:24:45+00:00 2026-06-07T02:24:45+00:00

Here is a simplefied code: Class Bar { static $foo; public function __construct(Foo $foo)

  • 0

Here is a simplefied code:

Class Bar
{
    static $foo;

    public function __construct(Foo $foo)
    {
        $this->foo = $foo;
        $this->loop($foo,1);
    }

    public function loop(Foo $foo, $index)
    {   

        $a = $foo->a;
        if ($index < 3)
        {
            $a[$index]->var2 += $a[$index]->var1;
            $a[$index]->var1 = 0;
            $foo->dump();
            $this->foo->dump();

            $this->loop(new Foo($a),++$index);
        }
    }
}

$p = array(
      new Baz(100,200),
      new Baz(300,400),
      new Baz(400,200),
      new Baz(600,400)
    );

new Bar(new Foo($p));

There are two problem with this code. maybe more;)

  1. It looks like that the reallocation of var1 has an affect on $foo. I would expect that after i passed $foo->a to $a it doesnt have.

  2. In the construct i assaigned $foo to $this->foo but after every loop it changes its value. Could some point me out where does $this->foo’s value change?

Some more Info
Definition of Foo Class

Class Foo
{
    public $a;
    public function __construct($a)
    {
        $this->a = $a;
    }
    public function dump($title="")
    {
        echo "<br/>================".$title."=============================";
        echo "<table>";
        for ($i=0; $i<sizeof($this->a); $i++)
        {
            echo "<tr>";
            echo "<td>" . $this->a[$i]->var1 . "</td>";
            echo "<td>" . $this->a[$i]->var2 . "</td>";
            echo "</tr>";

        }
        echo "</table>";
    }

}

The Baz Class

Class Baz
{
    public $var1;
    public $var2;
    public function __construct($var1, $var2)
    {
        $this->var1=$var1;
        $this->var2=$var2;
    }

}

The results of dumps:

================IM foo dump=============================
100 200
0   700
400 200
600 400

================Im $this->foo dump=====================
100 200
0   700
400 200
600 400

================IM foo dump=============================
100 200
0   700
0   600
600 400

================Im $this->foo dump======================
100 200
0   700
0   600
600 400
  • 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-07T02:24:46+00:00Added an answer on June 7, 2026 at 2:24 am

    The behaviour you are seeing is normal. When assigning objects to variables in PHP, you pass a reference, not the value; if you want to have copies instead of references, you have to create a clone.

    For instance, in the following scenario:

    $blub = new Bar();
    $a = $blub;
    $b = $blub;
    $c = $blub;
    

    $a, $b, $c are just “aliases” so to speak, that point to $blub; meaning setting any member of $a has a direct effect on $blub, and therefore $b and $c aswell.

    By using the keyword clone however you can achieve the result you are looking for:

    $blub = new Bar();
    $a = clone $blub;
    $b = clone $blub;
    $c = clone $blub;
    

    Here we have explicitly created independent copies, and thus modifying the contents of $a will not change $blub, $b, or $c.

    Finally here’s how your example class could look like implementing clones to work on standalone objects:

    Class Bar
    {
        public $foo;
    
        public function __construct(Foo $foo)
        {
            $this->foo = $foo;
            $this->loop(clone $foo, 1);
        }
    
        public function loop(Foo $foo, $index)
        {   
            $a = array();
    
            foreach($foo->a as $key => $subobject) {
                $a[$key] = clone $subobject;
            }
    
            if ($index < 3)
            {
                $a[$index]->var2 += $a[$index]->var1;
                $a[$index]->var1 = 0;
                $foo->dump();
                $this->foo->dump();
    
                $this->loop(new Foo($foo->a),++$index);
            }
        }
    }
    
    
    new Bar(new Foo($p));
    

    Update

    Simply cloning $foo was not enough due to the fact that an array of objects is passed around:

    $p = array(
          new Baz(100,200),
          new Baz(300,400),
          new Baz(400,200),
          new Baz(600,400)
        );
    

    Even if you clone the object which hosts the array, the objects themselves within the array still point to the same objects, since the array is just holding references; when an object is cloned, array members and their contents are copied, including references. It takes a bit to get your head around the concepts of references and values but eventually you’ll see everything fits like a puzzle 🙂

    Anyhow, in this specific case one has to clone the objects within the array before using them, and this is easily achieved with the following loop inside the Bar::loop function:

            foreach($a as $key => $subobject) {
                $a[$key] = clone $subobject;
            }
    

    See the class above for the complete edit.

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

Sidebar

Related Questions

here's the code (simplified): public abstract class PageBase implements Serializable { private static final
Here is a simplified code example of what I'm trying to do: public class
Here is my simplified code: // Domain models public class Order { public int
Folks, Here is a simplified code for my background thread: public class MyThread extends
Here is a simplified snippet of my code: <li id=work-5 class=work-5 class-B> <div>some other
Here's some (simplified) code for what I'm trying to do: class a: pass class
A very strange thing occured in my program. Here is the simplified code. class
So I've noticed some very weird behaviour from my program. Here's a simplified code
Here is a simplified version of the code I use. I need to rewrite
Here's what I'm trying to do, simplified: <input rel=TimeStart> // bunch of HTML code

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.