Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:12:47+00:00 2026-06-14T02:12:47+00:00

I have a question about pointer in PHP. In a A class i have

  • 0

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;
}

I use it in my code like this :

$day = new \DateInterval('P1D');
$a->startDate->add($day);

Result : “Cannot access protected property” (as expected)


if I try :

$day = new \DateInterval('P1D');

print_r($a->getStartDate());

$date = $a->getStartDate();
$date->add($day);

print_r($a->getStartDate());
die();

Result :

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
)

I modified a protected value without the setter

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.

Do you know why?

Thanks

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).

Thanks again !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:12:49+00:00Added an answer on June 14, 2026 at 2:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a question about this pointer, when an object is constructed, when
I have a question about pointer casting for C. if I have a function
I have a question about PHP and the use of pointers and variables. The
After reading a description about swapping pointer addresses on Stackoverflow, I have a question
I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
I have a question about use of the goto statement in C++. I understand
Possible Duplicate: question about copy constructor if I have this snippet of the code
I have a question regarding setting up a custom delegate class for use with
This is a very open question about a problem that we have. Basically, we
I have question about pointers in C. Each pointer has 4 bytes in memory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.