Menu

← Back to cast.*

Error Identifier: cast.double

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

$value = (float) new \stdClass();

Why is it reported? #

The expression being cast to float is of a type that cannot be converted to a float. PHP does not support casting certain types (such as objects that do not implement __toString() or other non-numeric types) to float, and attempting to do so will result in an error at runtime.

In the example above, an stdClass object cannot be cast to float.

How to fix it #

Extract the numeric value from the object or expression before casting:

 <?php declare(strict_types = 1);
 
-$value = (float) new \stdClass();
+$obj = new \stdClass();
+$obj->value = 3.14;
+$value = (float) $obj->value;

Or ensure the expression produces a type that can be cast to float, such as int, string, or bool:

 <?php declare(strict_types = 1);
 
-$value = (float) new \stdClass();
+$value = (float) '3.14';

How to ignore this error #

You can use the identifier cast.double to ignore this error using a comment:

// @phpstan-ignore cast.double
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: cast.double

Rules that report this error #

  • PHPStan\Rules\Cast\InvalidCastRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.