问题
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