instanceof expects an object instance, constant given

Description

Until PHP 7.3, PHP required an object as the left operand of instanceof. In recent versions, PHP does not emit an error anymore, and returns null.

Although the function reports a constant problem, the error is reported with literal values, and not with constants.

Constants were allowed to receive objects in PHP 8.1.

Example

<?php

null instanceof A::class;

?>

Solutions

  • Upgrade to PHP 7.3 or more recent.

  • Chech the left operand with is_object(), before using instanceof.