• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Type Operators instanceof is used to determine whether a PHP variable is an inst ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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
 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php高并发状态下文件的读写发布时间:2022-07-10
下一篇:
PHPstatic静态局部变量和静态全局变量总结发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap