Is this the only way to check if an object is an instance of a class, in my case of the DateTime class?
$cls = ReflectionClass("DateTime");
if (! $cls->isInstance( (object) $var ) ) {
// is not an instance
}
It seems a bit heavy to me.
Is this the only way to check if an object is an instance of a class, in my case of the DateTime class?
$cls = ReflectionClass("DateTime");
if (! $cls->isInstance( (object) $var ) ) {
// is not an instance
}
It seems a bit heavy to me.
You could try instanceofDocs...
if ($var instanceof DateTime) {
// true
}
See also is_aDocs:
if (is_a($var, 'DateTime')) {
// true
}
You can use get_class function like this:
<?php
$a = new DateTime();
if (get_class($a) == 'DateTime') {
echo "Datetime";
}
use \DateTime at the top of your file to make it look for DateTime in the root namespace (not your app's namespace).