Re: Typed constants revisited
| From: | Mark Niebergall | Date: | Fri, 25 Mar 2022 16:27:05 +0000 |
| Subject: | Re: Typed constants revisited | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117428@lists.php.net to get a copy of this message | ||
Guilliam,
On Fri, Mar 25, 2022 at 6:35 AM Guilliam Xavier <guilliam.xavier@gmail.com>
wrote:
> Hi Mark,
>
> On Wed, Mar 23, 2022 at 11:55 PM Mark Niebergall <mbniebergall@gmail.com>
> wrote:
>
>> (...)
>>
>> Another example I often see in my projects could be used with a similar
>> example:
>>
>> ```
>> abstract class Bird
>> {
>> public const bool CAN_FLY;
>> public const string FAMILY;
>> public function canFly(): bool
>> {
>> return self::CAN_FLY;
>> }
>> }
>> final class EmperorPenguin extends Bird
>> {
>> public const bool CAN_FLY = false;
>> public const string FAMILY = 'penguin';
>> }
>> ```
>>
>
> I had this "need" too (and used abstract static methods where the
> implementations just return a literal value...).
>
> Just 2 remarks: in abstract class Bird, shouldn't it be:
> - "*abstract* public const bool CAN_FLY;" (and same for FAMILY)
> - "return *static*::CAN_FLY;"
> ?
>
I intentionally left
abstract out of public const bool CAN_FLY; in the
abstract class for consistency with the implementation with interface,
which would also have to be public const bool CAN_FLY;. Currently
abstract is only used in front of methods `abstract function doThing():
bool;`. Open to discussion - which way is ideal or preferred? That could be
included as a subset of an RFC vote if a consensus during discussion isn't
reached.
Good correction, yes, return static::CAN_FLY; in that example in the
concrete child class.
>
> Regards,
>
> --
> Guilliam Xavier
>