Req #73604 [Opn]: Explicit type cast for class constants produces error

From: Date: Tue, 26 Oct 2021 13:30:55 +0000
Subject: Req #73604 [Opn]: Explicit type cast for class constants produces error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237385@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73604&edit=1

 ID:                 73604
 Updated by:         cmb@php.net
 Reported by:        anatoly dot pashin at gmail dot com
 Summary:            Explicit type cast for class constants produces
                     error
 Status:             Open
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 PHP Version:        7.0.13
 Block user comment: N
 Private report:     N

 New Comment:

For reference: <https://3v4l.org/0Wl9X>.


Previous Comments:
------------------------------------------------------------------------
[2016-11-25 17:57:39] stas@php.net

Some non-object casts can depend on run-time environment, such as casting double to string (depends
on precision).

------------------------------------------------------------------------
[2016-11-25 17:17:46] nikic@php.net

Constant expressions only support a limited set of operations. Casts are currently not part of this
set. However, apart from the (object) cast, there is no *specific* reason to disallow them, so
adding support for this shouldn't be a problem.

------------------------------------------------------------------------
[2016-11-25 08:33:23] anatoly dot pashin at gmail dot com

Description:
------------
Interpreter produces PHP Fatal error:  Constant expression contains invalid operations
in… because of explicit const value type cast.

But implicit is allowed and works as expected.

Yes, I undestand difference between + and (int) that int will
cast to integer and drop past-float part. But question why that's now allowed is still present.

Casting plain value rather than array value doesn't work too. Even if you cast integer to
integer.

Test script:
---------------
class Barcode
{
    const BARCODE_TYPES = [
        (int)\Stickers::BARCODE_TYPE_MEGAORDER,
        +\Stickers::BARCODE_TYPE_SHOP_ORDER,
    ];
}

Expected result:
----------------
All is OK and array value became int casted.

Actual result:
--------------
PHP Fatal error:  Constant expression contains invalid operations in…


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=73604&edit=1


Thread (1 message)

  • cmb@php.net
  • Unknown Message
    • cmb@php.net
« previous php.bugs (#237385) next »