0

I am a bit confused about this scenario here. Working with PHP 8.0.10. In a class a have a member and getter method

/**
 * @ORM\Column(type="datetime")
 */
private $gStart;

public function getStart(): ?\DateTimeInterface
{
    return $this->gStart;
}

Then I have another Method returning a value determined by using the $gStart variable:

/**
 * @return \DateTimeInterface
 */
public function getEnd() {
    $end = $this->getStart();
    $gInterval = new \DateInterval( 'P90D' );
    $end->add( $gInterval );
    return $end;
}

Now - after calling getEnd() the member $gStart has changed as if I worked on the reference. Why is that? What am i missing here? Shouldn't the code in getEnd() leaving the member $gStart untouched working on a copy?

user3440145
  • 793
  • 10
  • 34
  • Objects are assigned by reference https://3v4l.org/NCsJ9 You might want `clone`. – AbraCadaver Oct 11 '21 at 17:42
  • OK... Very clear - thank you. Just asking myself: Why don't I have VERY much more wrong behaviour if I did not take so much attention on that? Is there sth like "a rule" when to pay extra attention on how one is getting "values"? – user3440145 Oct 11 '21 at 17:48

0 Answers0