PHP instanceof 为真条件返回假 [英] PHP instanceof returns false for true condition

查看:53
本文介绍了PHP instanceof 为真条件返回假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑为什么 php 的 instanceof 运算符坚持认为这里的 LVALUE 不是定义类的实例,而 get_class 说它是.>

I'm thoroughly confused as to why php's instanceof operator insists that the LVALUE here is not an instance of the defined class when get_class says that it is.

class WhereIn {
    public function __construct($args) {
        echo "is instanceof: " . ($args[0] instanceof ActiveRecordField) . EOL;
        echo "get class: " . get_class($args[0]) . EOL;
    }
}

这个方法的输出是:

is instanceof: 
get class: ActiveRecordField

作为参考,我使用的是 PHP 5.6.9.

For reference, I'm using PHP 5.6.9.

推荐答案

如果在代码中使用了命名空间,则需要直接提供:

If you use namespaces in your code, you need to provide it directly:

if ($args[0] instanceof ActiveRecordField) // False

if ($args[0] instanceof \MyCompany\Classes\ActiveRecordField) // True

这篇关于PHP instanceof 为真条件返回假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆