2

I did see this question but it doesn't fully answer the question I have.

If I do this:

$obj1 = new ObjectOne();

and then this:

$obj2 = new ObjectTwo($obj1);
$obj2->someFunction();

Where someFunction() modifies the attributes of the object passed into it, will both $obj1 and $obj2->passedInObject both in effect be updated?

Community
  • 1
  • 1
bcmcfc
  • 25,966
  • 29
  • 109
  • 181

1 Answers1

4

I tried the following code, and it works as expected. The var in MyObject gets incremented in the original object.

<?php 
class MyObject {
     public $var;
}

class MyObjectTwo {
    public $objVar;
    function __construct($aObj1) {
        $this->objVar = $aObj1;
    }

    public function someFunction() {
        $this->objVar->var++;
    }
}

$obj1 = new MyObject();
$obj1->var = 5; // Originally set to 5

$obj2 = new MyObjectTwo($obj1);
$obj2->someFunction();
echo $obj1->var; // Prints 6
rcapote
  • 1,034
  • 7
  • 19