Indirect access to a protected member of an object in PHP
<p>I have a question about pointer in PHP.</p>

<p>In a A class i have : </p>

<pre><code>protected $startDate;
public function getStartDate()
{
    return $this-&gt;startDate;
}
public function setStartDate($startDate)
{
    $this-&gt;startDate = $startDate;
}
</code></pre>

<p>I use it in my code like this : </p>

<pre><code>$day = new \DateInterval('P1D');
$a-&gt;startDate-&gt;add($day);
</code></pre>

<p><strong>Result : "Cannot access protected property" (as expected)</strong></p>

<hr>

<p>if I try : </p>

<pre><code>$day = new \DateInterval('P1D');

print_r($a-&gt;getStartDate());

$date = $a-&gt;getStartDate();
$date-&gt;add($day);

print_r($a-&gt;getStartDate());
die();
</code></pre>

<p>Result : </p>

<pre><code>DateTime Object
(
    [date] =&gt; 2012-11-08 00:00:00
    [timezone_type] =&gt; 3
    [timezone] =&gt; Europe/Paris
)

DateTime Object
(
    [date] =&gt; 2012-11-09 00:00:00
    [timezone_type] =&gt; 3
    [timezone] =&gt; Europe/Paris
)
</code></pre>

<p>I modified a protected value without the setter</p>

<p>I think that I modified the date value because it was returned as a pointer by the getter method. 
I don't understand how I could modify a protected value without using a setter method.</p>

<p>Do you know why?</p>

<p>Thanks</p>

<p>Edit : Ok the member of my A class is a DateTime Object as I explained.
If I want it to be really protected, maybe I have to make a "timestamp" member and return a new DateTime of my timestamp (or clone the member).</p>

<p>Thanks again ! </p>
