PHP instanceof returns false for true condition

Deadly 提交于 2019-12-23 11:50:05

问题


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;
    }
}

The output from this method is:

is instanceof: 
get class: ActiveRecordField

For reference, I'm using PHP 5.6.9.


回答1:


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


来源:https://stackoverflow.com/questions/30278401/php-instanceof-returns-false-for-true-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!