Cannot use %s as value for class constant %s::%s of type %s

Description

This error appears when there is a mismatch between the value of a constant, and its declared type.

Note that there is not type juggling, so the value of the constant must match the type exactly. Consider that strict_types always applies in this case.

Example

<?php

class X {
    const int A = 'abc';
    const int B = '123';
    const int C = 3, D = 'abc';
}

?>

Solutions

  • Remove the constant type.

  • Replace the constant type.

  • Replace the constant value.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.