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

Cannot use "::class" on %s

Description

Since PHP 8.0, expr::class can be used on any object expression to retrieve its class name, not only on class names, self, static or parent. When the expression is a scalar literal, such as an integer, float, string or boolean, PHP can already tell at compile time that it will never hold an object, and rejects the construct right away instead of waiting for a runtime error. %s is replaced with a description of the offending value.

Example

<?php

echo 10::class;

?>

Alternatives

  • Only use ::class on a class name, or on an expression that can hold an object.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See