If I create and instance of the DateTime
class, then vardump()
it:
<?php
$date = new DateTime;
var_dump($date);
output:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2016-12-22 00:21:21.022426"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
In this case var_dump is not explicitly reporting that the values of date
or timezone
are public, but my experience with var_dump
is that unless it reports otherwise (i.e. with a label like [private:date]
), all properties are assumed to be public.
This implies to me that you should then be able to run code like
<?php
$date = new DateTime;
echo $date->timezone;
and the output should contain the string UTC
but instead it contains nothing (or more specifically the NULL
value).
I understand that this is not the correct way to use DateTime
for retrieving timezone, but having PHP report an object as having public properties that you cannot then access seems to violate a fundamental rule of objects, namely that they hold properties that - if public - you can access them.
Why does var_dump
report these properties, yet when I try to access them, they evaluate to NULL
?