Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110755 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53389 invoked from network); 28 Jun 2020 13:14:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Jun 2020 13:14:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0FFDF1804A8 for ; Sun, 28 Jun 2020 05:03:11 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 28 Jun 2020 05:03:10 -0700 (PDT) Received: by mail-lj1-f179.google.com with SMTP id n24so14920749lji.10 for ; Sun, 28 Jun 2020 05:03:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=buUY4+GXBVA31OnSoJUrD9+BeitX4lO2KITEvxkkJTw=; b=tOzvMQBmSVkRl2Il8KrUUq5cZXKwfmvsMCeDQHOQB87bV3n1lwoMjPt2XxkEMb8XHA qUaIdJ1DQr4uxh2e2sIIVBCEFhO0kSCYtlqtLj5O9vca1CO7qSUAD4ZxyPtQ4zGHAVQt Yn72ui/Wwp8QriGcOTHvKBN53KM8jgzHTtvDog9h8NRAZYmi/VgjWFbzVDThv/2wgTok r+09pgWw8Tg/BszwC54gWKjudFLjiDYfTd73syaquua5XMh8sAXFMKe73kZcs1Dn41he VHjm6/d9hN/MLQBedt7UZYA5C1f6KPDueti4S4R6PrPHKvPwoTDEt1v9IIU1GEZ1XooW /UKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=buUY4+GXBVA31OnSoJUrD9+BeitX4lO2KITEvxkkJTw=; b=gqIw6bioLkX9q1bcps0uwJn8SjVTGqntWfdL0lfSNrFp66tkGcZmilXG01motNRo5U Yt/gU+EjsczTVSpT34m0swq1o+snp9xmfZMxlwbHS2I5y0+eLXnlgE45zTwY+4apRmyz iX1m9gZkD1cPKkGZOq6SDHO9HlsGwUJZjmnS+V7wy/dvImm81ViOyPFfaS9AL978omhg d/QgP3zwH75LN1A3br79+F/ctSJz9eDraOJe5fhOn7PtLyakz1s9Rsh3mTym3BPWz6R5 URJSG+ToFziylzxOaC4+7JrH7SoJU8BECBhNydOhuF395M6XxLaCFnYAh5QmUIRmyC0m 8cNA== X-Gm-Message-State: AOAM5320EERAFWblwbPC0tDZPYCtpTpyTpDNpq5grEtezN9NajmOH9Uv IA6j4jbZCC3CZubi+BXwmKl7meKuxnd1BcLKTnHyfSZI X-Google-Smtp-Source: ABdhPJxaHFnz+nxn388RrJAEdfPZypiRyE6sXA7f9qN61ZqDE5qTrOrqbC0MNeYkF1yeg3iQQk9pU7rQp+exbyPYmiY= X-Received: by 2002:a2e:94c9:: with SMTP id r9mr5599133ljh.445.1593345786731; Sun, 28 Jun 2020 05:03:06 -0700 (PDT) MIME-Version: 1.0 Date: Sun, 28 Jun 2020 15:02:55 +0300 Message-ID: To: PHP Internals List Content-Type: text/plain; charset="UTF-8" Subject: Typed constants From: benas.molis.iml@gmail.com (Benas IML) Hey internals, Is there any particular reason as to why constants cannot be typed? For example: ``` class Test { // this is illegal public const int TEST = 1; } ``` Having typed constants would be quite beneficial. First of all, we would obviously be more consistent with properties and functions ;) But also, we could ensure that the correct type is retained during inheritance. For example: ``` class Test { public const TEST = 0; } class Test2 extends Test { // this is legal (even though the type is different) public const TEST = 'abc'; } ``` ...but with typed constants, this would be possible: ``` class Test { // this is legal public const int TEST = 0; } class Test2 extends Test { // this is illegal since the type is not declared public const TEST = 'abc'; } class Test3 extends Test { // this is illegal since the type is not an integer public const string TEST = 'abc'; } class Test4 extends Test { // this is illegal since the value is not an integer public const int TEST = 'abc'; } class Test5 extends Test { // this is legal public const int TEST = 1; } ``` Moreover, even the PHP manual (e. g. https://www.php.net/manual/en/class.reflectionclass.php) and numerous RFCs (e. g. https://wiki.php.net/rfc/attribute_amendments) specify the type for constants. This may confuse newcomers since that is actually not allowed in PHP. I am prepared to do all of the hard work and implement this myself, if other internals also find this proposal a good idea. Best regards, Benas Seliuginas