php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73604 Explicit type cast for class constants produces error
Submitted: 2016-11-25 08:33 UTC Modified: 2021-10-27 09:14 UTC
From: anatoly dot pashin at gmail dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.0.13 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
43 - 10 = ?
Subscribe to this entry?

 
 [2016-11-25 08:33 UTC] 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…

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-25 17:17 UTC] nikic@php.net
-Type: Bug +Type: Feature/Change Request -Package: PHP Language Specification +Package: Scripting Engine problem
 [2016-11-25 17:17 UTC] 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 17:57 UTC] stas@php.net
Some non-object casts can depend on run-time environment, such as casting double to string (depends on precision).
 [2019-02-15 03:36 UTC] carusogabriel@php.net
-Summary: Excplicit type cast for class constants produces error +Summary: Explicit type cast for class constants produces error
 [2021-10-26 13:30 UTC] cmb@php.net
For reference: <https://3v4l.org/0Wl9X>.
 [2021-10-27 09:14 UTC] anatoly dot pashin at gmail dot com
For reference: https://3v4l.org/Guplu
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 21:01:29 2024 UTC