Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117437 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 3719 invoked from network); 28 Mar 2022 11:26:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2022 11:26:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2303A1804AB for ; Mon, 28 Mar 2022 05:54:09 -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, T_SCC_BODY_TEXT_LINE 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-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 ; Mon, 28 Mar 2022 05:54:08 -0700 (PDT) Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-2e592e700acso147535707b3.5 for ; Mon, 28 Mar 2022 05:54:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VoolWhVmMCNlxeWblXage4/KU6QBwvDMXdIQdgm9ihM=; b=fZ2R6GnMTcFj6zNFieuhHS7Jfi3Hych8VhbIaN6ZjNokMPvOb17DsAy3EZ/mArNP0e 2B1llH05f9JjkWq7LdZqSca/7rXWmVdg9t6hpup+plbUqEqVOSgiOpqstnua5R5bTqgQ Tq2HYe+HoPBxRMLifwTarXJysFObuHEagukjsDJWZzaXJyaeLjGyU9/paZECQqIgbXYP NmKNvtkH9DZJURj4Y4Ql20CiKYQMdzcfcmKtG3j6D+MNbgXRaQqtWGeF7xA666oboDcW ge5eCpRK0Dgnl0PanJlz2tKxV2BHyGLfRa+RIC8JOFpA/4K96eYMdm2eZt1OVWam4zzy +9Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VoolWhVmMCNlxeWblXage4/KU6QBwvDMXdIQdgm9ihM=; b=R7+wjwL33rXigW+ApH9S8mMfQVLFq0CQdmicmLbwW3X0NH64NuM21bd+czO7Nd3O23 xWYEa9C2QWAYnhwRLUw90Ye7NWJquwExhqMeEg6xOlDyZUe9KmrqJ5NizQSOEZYF/ex/ i1ndb1fGIkq8l6Wn8M7lKXQsb//xGwtBhes6HBlSycENeSCLymEHtSZ7ORf+Mw9wwkZV EXHoRn2zwrsjN1RulNe+YH5MsambqGCuEjuZw45Q9QAGMt9emQLv0ToYBneDS9HXzS4O zDKpjKy1rhT9zZaue+SpRDwqsg2f2DEpJEgG3NE2ycVEiCdxqfNJ6hzMbnk/WxH0iByX u26Q== X-Gm-Message-State: AOAM530E+1Dc629z9GHwwi4luJs3fdKjQRGEp2c3XOWZxNwOsu0M847K hUFzc6/7XmKgCFDm/89v7Nc7TeXUpG4TJTFGBpQhM+wgzQ== X-Google-Smtp-Source: ABdhPJzaEw595I/rWVtIyIo2dU4a1CGNcrSdn3mnE8zXgsCMOaSckTC/8svrIUC17sZeAwvJp87o4F98oJT8P7sGA3Q= X-Received: by 2002:a81:c54a:0:b0:2d6:435a:5875 with SMTP id o10-20020a81c54a000000b002d6435a5875mr24212538ywj.181.1648472047958; Mon, 28 Mar 2022 05:54:07 -0700 (PDT) MIME-Version: 1.0 References: <76c399cb-fb29-4583-a212-8eb69740c96b@www.fastmail.com> In-Reply-To: Date: Mon, 28 Mar 2022 14:53:56 +0200 Message-ID: To: Mark Niebergall Cc: php internals Content-Type: multipart/alternative; boundary="000000000000d73e8405db46ce8b" Subject: Re: [PHP-DEV] Typed constants revisited From: guilliam.xavier@gmail.com (Guilliam Xavier) --000000000000d73e8405db46ce8b Content-Type: text/plain; charset="UTF-8" > Constants are not abstract in an interface - they must be assigned a > value. Only classes and methods can be abstract. Within an abstract class > it is not valid to have an abstract property. Properties can be defined > `protected int $id;` and optionally assigned a value `protected int $id = > 5;`, but cannot be `abstract protected int $id;`. > That's the *current* state of the language indeed, but to me, [part of] your proposal looks like introducing "abstract constants"... Maybe I'm misunderstanding? > So to me it makes more sense to have constants follow the same syntax as > properties `public const bool CAN_FLY;` without the `abstract` keyword. > > An example: > > ``` > abstract class Bird > { > public const bool CAN_FLY; > protected bool $isExtinct; > ``` > > This allows for similar behavior, similar requirements, and similar syntax > - consistency ftw! > For similarity/consistency, the `$isExtinct` property should probably be [`public` and] `static` (actually `readonly` too, but cannot be both). But, again, we can also see some similarity with `static` *methods*, e.g.: ``` abstract class Bird { abstract public const bool CAN_FLY; abstract public static function isExtinct(): bool; } class Dove extends Bird { // NOTE: the following two lines are required (in the class definition) public const bool CAN_FLY = true; public function static isExtinct(): bool { return false; } } // var_dump(Dove::CAN_FLY); // var_dump(Dove::isExtinct()); ``` Besides, an uninitialized property must be initialized before first read, but is *not* required to be overridden/redefined (with a value) in a child class; so in this example (still hypothetical): ``` abstract class Bird { public const bool CAN_FLY; public static bool $isExtinct; } class Dodo extends Bird { // NOTE: the following two lines are commented out // public const bool CAN_FLY = false; // public static bool $isExtinct = true; } var_dump(Dodo::CAN_FLY); var_dump(Dodo::$isExtinct); ``` where would the [missing value for constant] error be: on the definition of class Dodo (like for an unimplemented abstract method), or only when trying to access Dodo::CAN_FLY (like for an uninitialized property)? > > There seems to be interest and good use cases (thanks Sara for the good > practical example!). At this point I'm going to work on a new RFC with all > the details and feedback from this discussion. > > Thanks & good luck! =) -- Guilliam Xavier --000000000000d73e8405db46ce8b--