I have a question about pointer in PHP.
In a A class i have :
protected $startDate;
public function getStartDate()
{
return $this->startDate;
}
public function setStartDate($startDate)
{
$this->startDate = $startDate;
}
$day = new \DateInterval('P1D');
$a->startDate->add($day);
$day = new \DateInterval('P1D');
print_r($a->getStartDate());
$date = $a->getStartDate();
$date->add($day);
print_r($a->getStartDate());
die();
DateTime Object
(
[date] => 2012-11-08 00:00:00
[timezone_type] => 3
[timezone] => Europe/Paris
)
DateTime Object
(
[date] => 2012-11-09 00:00:00
[timezone_type] => 3
[timezone] => Europe/Paris
)
Objects are always passed by reference. To be more exact, there's only one object, and a variable just holds a reference to the object. By assigning the variable value to another variable, you copy the value, but that value is only a reference to the object.
In short: When working with objects in PHP, you're always passing around the object and all modifications to it will be visible to everyone who can see the object.
If you want to break that reference, clone
the object.