Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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;

?>

Alternatives

  • Upgrade to PHP 7.3 or more recent.
  • Chech the left operand with is_object(), before using instanceof.