Re: Typed constants revisited
| From: | Mark Niebergall | Date: | Fri, 25 Mar 2022 18:52:51 +0000 |
| Subject: | Re: Typed constants revisited | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117432@lists.php.net to get a copy of this message | ||
On Fri, Mar 25, 2022 at 10:55 AM Guilliam Xavier <guilliam.xavier@gmail.com>
wrote:
> 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.
>>
>
> I understand, but note that methods are implicitly abstract in an
> interface, but it must be explicit in an abstract class; and since I see
> the proposed feature mainly as a "replacement" for abstract static methods
> [whose all implementations just return a literal value]... (anyway, not
> super important)
>
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;`. 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!
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.