Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110757 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60663 invoked from network); 28 Jun 2020 13:47:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Jun 2020 13:47:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E18461804C8 for ; Sun, 28 Jun 2020 05:35:53 -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,HTML_MESSAGE, 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-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) (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:35:53 -0700 (PDT) Received: by mail-lj1-f173.google.com with SMTP id n23so15056351ljh.7 for ; Sun, 28 Jun 2020 05:35:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=QeFuAJf+w281ukSrhe1WcSq20NDEoStHaKQIMXRudsM=; b=JA9cl+wWVqxK13qdKt3PuFh86NN+Mhlg2vjfMt1hALaljnrI2TWvV05SCzYi3nav9Q 6BlGTX9Hs4+ollMePbR3CBJFUOxTsHBxACQP31Du+pv+jhN6W1iDRGOVRBq9c98UWEsY aRf/LvCRsHeo0AM2TnOF5feZGwwBFk0Bz9XtGKB5pyCPHLt8PRQfX5Mzn8mNSYskOCn8 sU3iOBpeP1142CYN2de+hwkT/8+4CNDAReN9xLvgi8KVCn66AybYAEijSrMJdLnJAav+ 4dwUpsolBB8DR8pF4GRybJJDZPMBeJ6xoiADzedQpKqHkrxvq6GFmx8WLRPq3qahRH1F pcng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=QeFuAJf+w281ukSrhe1WcSq20NDEoStHaKQIMXRudsM=; b=WVPD7gysRQaBw2dm0HM30Jcq8/EtAR3E/S5lETtVcZo1RuCogog3Tx/e9UMsW3SOnU //FfnnB1My1xjTB/sEL4c0rS2p3CYsPTPT7yLxl3qUc/cbyyXFDFD6N1/95zSzqvVxH+ avlpugRjk75ce9c2Gt9ByOGY3eIDTQW0f0smAABN7qfqslVkkbbypCv4VqUatdq9R2iR HzUym6oKpzm0IdC0imcSCX6um1xHRVaRTVPelVLw0hvAhv6S5OAMiRytFLkdO8UjPDxz 5i1xFAP8Rj3ZZppkEFqtzjThjN9DVBEyY4DjLaTs99vOgUKM7rB5GMH26T8ywMIoe1PB tH6Q== X-Gm-Message-State: AOAM530l7hvVu8QWaPEO1JMKQD8IHDj0D0Dsv9nbbQvnPm9uqrvnnYQy WgHC39rb3Xx89U9IsdbAbtx4Tq5+/utfeFlaHyk= X-Google-Smtp-Source: ABdhPJxqgxTGFdJN/S8YXV4uEJIq/eycrWWaFJZiZ1wo5a6I5xkmbZ16rzp3NfbtECS3O/fDpLtB+HCNE/eK6DY1Tlc= X-Received: by 2002:a2e:6e05:: with SMTP id j5mr5716506ljc.135.1593347749942; Sun, 28 Jun 2020 05:35:49 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 28 Jun 2020 14:35:34 +0200 Message-ID: To: Benas IML Cc: PHP Internals List Content-Type: multipart/alternative; boundary="000000000000a39a4d05a9242e51" Subject: Re: [PHP-DEV] Typed constants From: nikita.ppv@gmail.com (Nikita Popov) --000000000000a39a4d05a9242e51 Content-Type: text/plain; charset="UTF-8" On Sun, Jun 28, 2020 at 2:03 PM Benas IML wrote: > 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 > Typing constants isn't terribly useful, because the type of the constant is already determined by its immutable value, which is not the case for property types or function return types. However, I do support allowing types for class constants in the interest of overall language consistency. Nikita --000000000000a39a4d05a9242e51--