PHP 8 call by reference unpurposly?

44 Views Asked by At

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?

0

There are 0 best solutions below