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. Windows环境下安装PIL(Python Imaging Library)库

    微信小程序--跳一跳最近火了一把,于是整了个辅助进行试玩,不过在运行程序过程中出现了个报错如图所示: 显然是缺少PIL(Python Imaging Library)库文件,于是通过pip命令行进行安 ...

  2. lua -- 系统提示框

    -- -- Author: chentong -- Date: 2014-3-24 -- 系统提示: -- 先做个简单的,在中上位置出现提示,逐渐消失,如果有新提示,则直接删除旧提示. -- 规则: ...

  3. 【GMT43智能液晶模块】例程一:ARM驱动LED

    实验原理: 通过STM32的一个GPIO驱动一个红色LED,GPIO为推挽输出模式,采用灌电流 方式与LED连接,基于STemWin人机界面通过按钮控制GPIO高.低电平输出,从而 控制LED亮灭. ...

  4. Java知多少(56)线程模型

    Java运行系统在很多方面依赖于线程,所有的类库设计都考虑到多线程.实际上,Java使用线程来使整个环境异步.这有利于通过防止CPU循环的浪费来减少无效部分. 为更好的理解多线程环境的优势可以将它与它 ...

  5. 跨浏览器的placeholder-jQuery版(jQuery插件EnPlaceholder)

    案例:整搜索框,需要默认占位符为"请输入关键词",获取焦点时,占位符消失或不可用(不影响正常输入),丢失焦点后,若用户无内容输入,占位符继续出现,继续占位.这种代码我想前端们已经很 ...

  6. jenkins 启动slave时,找不到合适的java程序

    今天添加slave 又不成功,我用右键,发现随意点了java的那个打开,结果刚弹出来就关闭了窗口,显示未启动成功. 真是小白的想法,不过跟工具打交道久了,多少逻辑上想一下不对. 一定是用了不合适的程序 ...

  7. ABBYY OCR技术教电脑阅读缅甸语(上)

    缅甸联邦共和国,原名缅甸,是东南亚的一个国家,从1962年到2010年,缅甸一直被政变后上台的军政府统治,直至最近5年它才对外界开放,与其他国家建立了贸易与文化联系. 缅甸语由很多方言组成,但所有方言 ...

  8. [原]VS2012编译GLEW 1.11

    1.到http://glew.sourceforge.net/下载源代码 2.使用vs2012打开build下vc6的glew.dsw ,自动生成2012工程(一路点确定)特别注意:不要使用build ...

  9. [GAN] Generative networks

    中文版:https://zhuanlan.zhihu.com/p/27440393 原文版:https://www.oreilly.com/learning/generative-adversaria ...

  10. jdk 自带的数据库Derby使用

    ij是derby自带的一个功能强大的数据库管理工具,可以进行很多数据库管理的操作,包括创建数据库, 启动/关闭数据库,执行SQL脚本等.完成准备工作后,就可以启动并使用ij工具了. 在cmd中输入如下 ...