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: 2019-02-15 03:36 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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Sep 15 20:01:27 2019 UTC