.. _object-casts-are-not-supported-in-this-context:
Object casts are not supported in this context
----------------------------------------------
.. meta::
:description:
Object casts are not supported in this context: All cast operators are not supported in a static constant expression.
:og:image: https://php-errors.readthedocs.io/en/latest/_static/logo.png
:og:type: article
:og:title: Object casts are not supported in this context
:og:description: All cast operators are not supported in a static constant expression
:og:url: https://php-errors.readthedocs.io/en/latest/messages/object-casts-are-not-supported-in-this-context.html
:og:locale: en
:twitter:card: summary_large_image
:twitter:site: @exakat
:twitter:title: Object casts are not supported in this context
:twitter:description: Object casts are not supported in this context: All cast operators are not supported in a static constant expression
:twitter:creator: @exakat
:twitter:image:src: https://php-errors.readthedocs.io/en/latest/_static/logo.png
.. raw:: html
Description
___________
All cast operators are not supported in a static constant expression. This applies to all casts operations: ``(int)``, ``(string)``, ``(bool)``, ``(array)``, ``(object)``, until PHP 8.5. In PHP 8.5 and later, only ``(object)`` is not supported.
They look like any other operator, such as ``+`` or ``.``, but they require data manipulations which are incompatible with constant expressions.
While it may be useless to apply a cast to a literal value, it might be useful to use it with another expression or another constant.
Example
_______
.. code-block:: php
Solutions
_________
+ Create a value directly with the target type.
Changed Behavior
________________
This error may appear following an evolution in behavior, in previous versions. See ` `_.