阅读量:5
在PHP中,instanceof关键字用于检查一个对象是否属于某个类或接口的实例
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
if ($dog instanceof Animal) {
echo "The object is an instance of Animal.";
} else {
echo "The object is not an instance of Animal.";
}
?>
在这个例子中,我们定义了两个类:Animal和Dog。Dog类继承了Animal类。然后我们创建了一个Dog类的实例,并将其赋值给变量$dog。
接下来,我们使用instanceof关键字检查$dog是否是Animal类的实例。如果是,我们输出"The object is an instance of Animal.“,否则输出"The object is not an instance of Animal.”。
在这个例子中,输出将是:“The object is an instance of Animal.”,因为Dog类是Animal类的子类,所以$dog是Animal类的实例。
以上就是关于“instanceof php如何使用正确”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm