私の DateTime クラスの場合、これがオブジェクトがクラスのインスタンスであるかどうかを確認する唯一の方法ですか?
$cls = ReflectionClass("DateTime");
if (! $cls->isInstance( (object) $var ) ) {
// is not an instance
}
私には少し重いようです。
私の DateTime クラスの場合、これがオブジェクトがクラスのインスタンスであるかどうかを確認する唯一の方法ですか?
$cls = ReflectionClass("DateTime");
if (! $cls->isInstance( (object) $var ) ) {
// is not an instance
}
私には少し重いようです。
instanceof
あなたはDocsを試すことができます...
if ($var instanceof DateTime) {
// true
}
is_a
ドキュメントも参照してください:
if (is_a($var, 'DateTime')) {
// true
}
if ($var instanceof DateTime)
次のように get_class 関数を使用できます。
<?php
$a = new DateTime();
if (get_class($a) == 'DateTime') {
echo "Datetime";
}
インスタンスオブはどうですか