Hey Mark,

On Wed, Mar 30, 2022 at 4:01 PM Mark Niebergall <mbnieberg...@gmail.com>
wrote:

> Alex,
>
> On Tue, Mar 29, 2022 at 10:35 PM Alexandru Pătrănescu <dreal...@gmail.com>
> wrote:
>
>> Hey Mark,
>>
>>
>> On Wed, Mar 30, 2022 at 6:03 AM Mark Niebergall <mbnieberg...@gmail.com>
>> wrote:
>>
>>>
>>> I have updated the RFC https://wiki.php.net/rfc/typed_class_constants
>>> with
>>> more details and examples from this thread, and updated the RFC status to
>>> Under Discussion. Hopefully the updated RFC helps answer questions and
>>> clarifies what the proposal includes.
>>>
>>
>> Thanks for the RFC and the drive here.
>> I personally see the benefit and I think it would be a nice addition.
>>
>> I think you should also update the "Supported types" section.
>> Starting with enums, constants can also be objects. Once a good technical
>> solution is found, any object would be supported probably
>> https://wiki.php.net/rfc/new_in_initializers#future_scope
>> I think that only void, never and callable types should not be supported,
>> just like on properties.
>>
>>
> I have updated the "Supported types" to list out all types that are
> supported and which types are not supported.
>
> Constants cannot be objects since objects are mutable, so typed constants
> will not be allowed to be an enum (which is technically an object). A typed
> constant _may_ be an enum value though, so the following example will be
> valid:
>
> ```
> enum Fruit
> {
>     case Apple;
>     case Banana;
> }
>
> class Colors
> {
>     protected const string RED = Fruit::Apple;
>     protected const string YELLOW = Fruit::Banana;
> }
> ```
>

Actually, Fruit::Apple is an enum instance and that is an object that is of
Fruit enum type (Fruit class).
You can check this as it's already possible in PHP 8.1, even if it's not
yet possible to have them with type: https://3v4l.org/f4WIC

What you are referring to as enum value is actually a backed value for an
enum instance.
If you would define the enum as
```
enum Fruit: string
{
    case Apple = 'apple';
    case Banana = 'banana';
}
```
You could obtain the value with Fruit::Apple->value. But I'm not
referring to this.

So going back to the topic I mentioned, a constant can right now be an
object.
It can be only an enum instance for now but I believe that in the near
future we will be able to have any object there.

Also, self and parent would make sense for valid typed constants, just like
they do work on properties.

Regards,
Alex


>
>>
>>> Of note is the "Inheritance and variance" section, which details uses
>>> with
>>> abstracts and interfaces, plus the "Constant values" section which
>>> includes
>>> details about errors.
>>>
>>>
>>> >
>>> > --
>>> > Guilliam Xavier
>>> >
>>>
>>
>> Thanks,
>> Alex
>>
>

Reply via email to