w

0-instanceof is used to determine whether a PHP variable is an instantiated object of a certain class

1-instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class

2-Lastly, instanceof can also be used to determine whether a variable is an instantiated object of a class that implements an interface

http://php.net/manual/en/language.operators.type.php

http://php.net/manual/zh/language.operators.type.php

类型运算符

instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例

instanceof 也可用来确定一个变量是不是继承自某一父类的子类的实例

最后,instanceof也可用于确定一个变量是不是实现了某个接口的对象的实例

<?php

interface MyInterface
{
} class MyClass implements MyInterface
{
} $a = new MyClass; var_dump($a instanceof MyClass);
var_dump($a instanceof MyInterface);

bool(true) bool(true)

<?php

interface MyInterface
{
} class MyClass implements MyInterface
{
} $a = new MyClass;
$b = new MyClass;
$c = 'MyClass';
$d = 'NotMyClass'; var_dump($a instanceof $b); // $b is an object of class MyClass
var_dump($a instanceof $c); // $c is a string 'MyClass'
var_dump($a instanceof $d); // $d is a string 'NotMyClass'

bool(true) bool(true) bool(false)

instanceof does not throw any error if the variable being tested is not an object, it simply returns FALSE. Constants, however, are not allowed.

如果被检测的变量不是对象,instanceof 并不发出任何错误信息而是返回 FALSE。不允许用来检测常量。

<?php
$a = 1;
$b = NULL;
$c = imagecreate(5, 5);
var_dump($a instanceof stdClass); // $a is an integer
var_dump($b instanceof stdClass); // $b is NULL
var_dump($c instanceof stdClass); // $c is a resource
var_dump(FALSE instanceof stdClass);

bool(false) bool(false) bool(false)

Fatal error: instanceof expects an object instance, constant given
 
 

Type Operators instanceof is used to determine whether a PHP variable is an instantiated object of a certain class/a class that implements an interface的更多相关文章

  1. type 、instanceof、in 和 hasOwnproperty

    typeof可以检测的类型有:string.number.boolean.undefined.不可以用typeof检测null typeof也可以用来检测function,但是在IE8及跟早的浏览器中 ...

  2. java关键字extends(继承)、Supe(父类引用空间)、 This(方法调用者对象)、Instanceof(实例类型-判断对象是否属于某个类)、final(最终)、abstract(抽象) 、interface(接口)0

    java 继承使用关键字extends   继承的作用:减少代码量,优化代码 继承的使用注意点: 1子类不能继承父类的私有变量 2.子类不能继承父类的构造方法 3.子类在调用自己的构造方法时 会默认调 ...

  3. Jetty - Container源码分析

    1. 描述 Container提供管理bean的能力. 基于Jetty-9.4.8.v20171121. 1.1 API public interface Container { // 增加一个bea ...

  4. golang 自定义类型的排序sort

    sort包中提供了很多排序算法,对自定义类型进行排序时,只需要实现sort的Interface即可,包括: func Len() int {... } func Swap(i, j int) {... ...

  5. 23 The Laws of Reflection 反射定律:反射包的基本原理

    The Laws of Reflection  反射定律:反射包的基本原理 6 September 2011 Introduction 介绍 Reflection in computing is th ...

  6. 4、Type fundamentals

    1.All Types Are Derived from System.Object The CLR requires all objects to be created using the new ...

  7. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)

    一.结构 二.Hibernate支持的UserTypes接口  UserType —You can transform values by interacting with the plain JD ...

  8. Java里面instanceof怎么实现的

    开始完全一头雾水呀,后面看了Java指令集的介绍,逐渐理解了. https://www.zhihu.com/question/21574535/answer/18998914 下面这个答案比较直白 你 ...

  9. 【转载】逃离adapter的地狱-针对多个View type的组合实现方案

    英文原文:JOE'S GREAT ADAPTER HELL ESCAPE 转载地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015 ...

随机推荐

  1. Source Insight 中文注释为乱码解决办法(完美解决,一键搞定)

    我从网上查了一堆解决办法,但是都是2017年以前的解决方案,并且都是针对于source insight 3.5及以下版本的,目前SI软件版本都到4.0了,应该有新方法出现了. ------------ ...

  2. rsync用于数据迁移/备份的几个细节

    上周我们的一个GitLab服务频繁出现web页面卡死问题,得重启虚拟机才可恢复,但重启之后没多久又会卡死.后来发现是虚拟机的磁盘大小超过了2T,而虚拟机管理那层的文件系统是ext3,最大单文件只能支持 ...

  3. 随机获取一个集合(List, Set,Map)中的元素<转>

    import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Random; impo ...

  4. hadoop无法停止

    停止hadoop集群,运行命令 $ sh stop-all.sh 出现提示: no resourcemanager to stop host10: no nodemanager to stop hos ...

  5. 重新入坑-IntelliJ Maven

    写Restful的服务,使用IntelliJ+maven,发现有几个依赖总是没法配置好.通过检查POM.xml,发现犯了错误,<dependency>写到了<dependencies ...

  6. Spark学习笔记——Spark上数据的获取、处理和准备

    数据获得的方式多种多样,常用的公开数据集包括: 1.UCL机器学习知识库:包括近300个不同大小和类型的数据集,可用于分类.回归.聚类和推荐系统任务.数据集列表位于:http://archive.ic ...

  7. jdk和tomcat基本配置

    问题:前端采用grunt构建,后台采用java编写使用Eclipse或IntelliJ,把Tomcat嵌入到开发工具当中.问题一:在于是grunt编译之后生成的文件,每次都需要刷新项目文件夹,然后在刷 ...

  8. shell中的函数 shell中的数组 告警系统需求分析

     

  9. JS中lambda表达式的优缺点和使用场景(转)

    add by zhj: 最近在看ES6,看到了箭头函数,我个人感觉箭头函数适用于函数体中不用this的匿名函数,在箭头函数中使用this是一个坑 原文:http://ourjs.com/detail/ ...

  10. [TensorBoard] *Cookbook - Tensorboard

    Ref: https://www.tensorflow.org/get_started/summaries_and_tensorboard 可视化对于Training的重要性,不言而喻. 代码示范 # ...