I can print the $date object which is an instance of the DateTimeImmutable class. At least that is how I understand it. Inside there’s a date property, a timezone and a timezone_type property. I would like to access the date property of the date object but I don’t know how.
I’ve tried $date->date and a number of variations on that but that doesn’t work. I’m completely stuck. No idea how to proceed. This is what I have
function from(DateTimeImmutable $date): DateTimeImmutable
{
var_dump($date);
}
You cannot access private properties of objects. And in general you should not access properties of objects anyway, but rather use the methods they provide to access their state.
In particular, for Gigasecond you may want to have a look at the add() method since the purpose of the exercise is to add time to a given date.
Additionally, and possibly a bit off-topic: the DateTimeImmutable class offers immutable objects, which means that they’re not intended to be modified (so none of the methods it provides will modify the object itself; instead they will return a new object).
Immutable objects are useful in certain situations such as functional programming and parallelization (if objects always have the same state then code using them can run in parallel because their internal state does never depend on which moment in time you use them or what operations ran on them in the past).
I thought the DateTimeImmutable class had been specifically created for this exercise so I didn’t occur to me to look up it’s methods. Thank you so much, the link to the documentation is very helpful. My background is javascript, I’ve heard of private properties and such but have never really used them.