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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to