Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP instanceof returns false for true condition

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.

like image 707
kellanburket Avatar asked May 16 '15 16:05

kellanburket


1 Answers

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
like image 68
Pmpr.ir Avatar answered Sep 22 '22 20:09

Pmpr.ir