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.