website/errors/greater.invalid.md
<?php declare(strict_types = 1);
function doFoo(\stdClass $object, int $number): void
{
if ($object > $number) {
// ...
}
}
The greater-than comparison (>) between an object and an integer results in an error in PHP. Since PHP 8.0, comparing incompatible types with the > operator throws a TypeError. In this example, comparing a \stdClass object with an int is not a valid operation.
Compare values of compatible types instead. Extract a comparable value from the object first:
<?php declare(strict_types = 1);
function doFoo(\stdClass $object, int $number): void
{
- if ($object > $number) {
+ if ($object->value > $number) {
// ...
}
}
Or ensure both operands are of comparable types:
<?php declare(strict_types = 1);
-function doFoo(\stdClass $object, int $number): void
+function doFoo(int $a, int $number): void
{
- if ($object > $number) {
+ if ($a > $number) {
// ...
}
}