Reputation: 1074
I tried using a check in PHP 5.5 to see if a variable contains an instanceof DateTime. is_object($value) passes but instanceof DateTime fails
if (is_object($value) && $value instanceof DateTime )
{
$value = $value->format(AppConstant::DATETIME_FORMAT_MYSQL);
}
However, this works just fine.
if (is_object($value) && is_a($value, "DateTime") )
{
$value = $value->format(AppConstant::DATETIME_FORMAT_MYSQL);
}
From everything I've read, it seems like I should be able to do the former. Why does instanceof
not work in this case?
Upvotes: 1
Views: 961
Reputation: 28941
My guess is you're inside a namespace, in which case you need to explicitly specify the root namespace for DateTime
:
if (is_object($value) && $value instanceof \DateTime )
Or, of course you can specify at the top of your file:
use DateTime;
...
if (is_object($value) && $value instanceof DateTime )
Upvotes: 7