2

When I try converted DateTime object to be an array, then I dump the variable it will show like below

$a = new DateTime('now');
var_dump((array)$a);

output

array(3) {
  ["date"]=>
  string(19) "2013-12-15 18:24:56"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Asia/Bangkok"
}

from PHP official document explains as below,

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name.

However, when see in DateTime manual, it haven't defined this 3 properties at all. Even more than that, if I try to set this 3 properties, it won't return the error, but it also won't have change reflect on the object.

php > $a->date = '2013-12-11';
php > var_dump((array)$a);
array(3) {
  ["date"]=>
  string(19) "2013-12-15 18:24:56"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Asia/Bangkok"
}

My question is, how to make object be able to casting to be array like this behaviour?

[Update] Even you use get_object_vars, on the document, you will see as following

Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property has not been assigned a value, it will be returned with a NULL value.

It will show you that if the "date", "timezone_type", "timezone" is public properties, why they are not be able to set.

scalopus
  • 2,640
  • 3
  • 19
  • 32
  • @kingkero No, it isn't duplicated. Except you can explain why I set the property of Datetime but it is not reflect on the object itself. – scalopus Dec 15 '13 at 11:53
  • When working with `DateTime` it seems you need to use its methods (`setDate()`, `setTime()`, etc.). If your question is, why you cannot use the "regular way" (directly accessing the attributes), please rephrase the question – kero Dec 15 '13 at 12:01
  • I know about that function. as a question, I didn't metion about the way to set DateTime object. But it is about when it casting to array, it doesn't show value from public property. and even it is public property, it also like read-only attribute. / @user555 it isn't a bug. if you see in bug tracker, it won't be able to access the property at all. – scalopus Dec 15 '13 at 12:09
  • @scalopus `'date'`, `'timezone_type'` and `'timezone'` aren't public properties. You can only see them when doing a `var_dump()` — it's a side-effect. – user555 Dec 15 '13 at 12:14
  • @scalopus The same thing happens when you cast the DataTime object as an array. They are not public members of DataTime, you can't set or get them. They are "magically" created when you do a `var_dump()` or cast the object as an array. As such it's a side-effect. – user555 Dec 15 '13 at 12:29

1 Answers1

-1

Here is the same question How do I convert an object to an array?

You try:

$a = new DateTime('now');
$arr = get_object_vars($a);
print_r($arr['date']);

But you can not set values to result array (it is not connected to an object it is a copy). This conversion work only one way.

Community
  • 1
  • 1
klipach
  • 818
  • 7
  • 21