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. 设置Nginx以列表方式显示网站内容

    服务器目录内容: 访问该页面时,将所有文件和目录按列表方式显示 nginx配置文件

  2. 这可能由 CredSSP 加密 oracle 修正引起的。

    某天在与服务器进行远程连接时,遇到了以下错误: 发生了身份验证错误. 不支持请求的函数. 远程计算机: <主机名> 这可能由 CredSSP 加密 oracle 修正引起的. 有关更多信息 ...

  3. Eclipse和MyEclipse工程描述符.classpath和.project和.mymetadata详解(转)

    转自:http://blog.csdn.net/zygsee/archive/2009/12/22/5046100.aspx 有时候在一个Java工程里我们需要加入第三方jar包,这时你加入的最好相对 ...

  4. 升级python(linux)

    查看系统当前python版本 2: [root@wangyuelou ~]# python     Python 2.4.3 (#1, May  5 2011, 16:39:10)     [GCC ...

  5. mysql服务启动、停止、重启

    如何启动/停止/重启MySQL 一.启动方式 1.使用 service 启动:service mysqld start 2.使用 mysqld 脚本启动:/etc/inint.d/mysqld sta ...

  6. 未能加载文件或程序集"CheckRegister"或它的某一个依赖项.参数错误. (异常来

    报“未能加载文件或程序集“CheckRegister”或它的某一个依赖项.参数错误”的解决方法 问题如下所示: 未能加载文件或程序集“CheckRegister”或它的某一个依赖项.参数错误. (异常 ...

  7. Tomcat配置多个域名绑定到不同项目

    <Host name="www.dn-serve.com" appBase="webapps" unpackWARs="true" a ...

  8. Java编程的逻辑 (84) - 反射

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  9. c# 使用GDAL处理大图

    注意问题: 1.GDAL 使用官网生成好的dll,必须把Bin目录下的dll一并加到执行目录下去,否则会出错.  2. 用环境变量设置引用路径可以避免一大堆dll放一起.代码如下: /// <s ...

  10. T4 生成数据库实体类

    来源不详,整理如下: <#@ template language="C#" debug="True" hostspecific="True&qu ...